JavaScript程序的执行时间

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

*是否可以对这样的程序执行沙箱,以便在不同的运行中获得不变或更less的执行时间?

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

看看node自己的process.hrtime() 。

这将在[秒,纳秒]元组数组中返回高分辨率实时。 这是相对于过去的任意时间。 这与时间无关,因此不受时钟漂移的影响。 主要用于衡量间隔期间的performance。 您可以将先前调用的结果传递给process.hrtime()以获取差异读数,这对基准和测量间隔很有用

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); 

阅读这个有用的博客文章