dmonji
dmonji

Reputation: 131

Execution time of JavaScript programs

I am dealing with execution time of JavaScript programs. When I run them with nodejs/v8, time command on shell returns varying execution times.

*Is it possible to sandbox the execution of such program so that I will get constant or less varying execution times across different runs?

*Is there any other way to check the execution time of these programs using nodejs/v8?

Upvotes: 2

Views: 181

Answers (1)

Philip O'Brien
Philip O'Brien

Reputation: 4266

Have a look at node's own process.hrtime().

This returns high-resolution real time in a [seconds, nanoseconds] tuple Array. It is relative to an arbitrary time in the past. It is not related to the time of day and therefore not subject to clock drift. The primary use is for measuring performance between intervals. You may pass in the result of a previous call to process.hrtime() to get a diff reading, useful for benchmarks and measuring intervals

var time = process.hrtime();
// [ 1800216, 25 ]

setTimeout(function() {
  var diff = process.hrtime(time);
  // [ 1, 552 ]

  console.log('benchmark took %d nanoseconds', diff[0] * 1e9 + diff[1]);
  // benchmark took 1000000527 nanoseconds
}, 1000);

Have a read of this useful blog post

Upvotes: 1

Related Questions