开发者

Getting the CPU time in JavaScript

Is there a way to get the time a piece of JavaScript actually runs on the CPU, like with the clock() and getrusage(开发者_开发百科) functions in C? Using the normal Date() way only gives the wall time, and for performance measurements the CPU time would be much more useful.

Note that I'm not looking for profiling tools, I want to reduce system noise for automated testing and not optimize specific functions.


No, you can't. Date is the only thing the standard JavaScript library provides.


Most modern browsers, notably WebKit based ones, have Javascript profiling built in (e.g. in the Web Inspector). This should be even more useful than clock().


This may be old, but I bumped here. So might as well share what I used.

There has now been a lot of updates with nodejs, and Node.js create a performance API NodeJS Performance.

var t0 = performance.now()
doSomeFunction()
var t1 = performance.now()
console.log( `performance: ${t1-t0}` )
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜