JavaScript 个程序的执行时间

Execution time of JavaScript programs

我正在处理 JavaScript 程序的执行时间。当我 运行 他们 使用 nodejs/v8,shell returns 上的时间命令不同的执行时间。

*是否可以对此类程序的执行进行沙箱化,以便我在不同的 运行 秒内获得恒定或变化较小的执行时间?

*有没有其他方法可以使用nodejs/v8检查这些程序的执行时间?

看看节点自己的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);

阅读 this useful blog post