Javascript:在线测量代码执行时间

时间:2013-06-27 09:17:14

标签: javascript performance performance-testing

我遇到了测试某些代码变体(本机/插件)性能差异的需求。

是否有在线服务,如jsbin,jsfiddle执行,我可以在其中放入代码, 像

// BEGIN
var bla;
jQuery.map(bla, function(){});
// END

并获得执行时间?

4 个答案:

答案 0 :(得分:40)

一个选项是

jsperf.com

//works in chrome and firefox
console.time("myCode"); // 'myCode' is the namespace
//execute your code here
console.timeEnd("myCode");

var startTime = window.performance.now();
//execute your code here
console.log(window.performance.now() - startTime);

答案 1 :(得分:8)

使用'用户时序API'这是一种现代的做法:
http://www.html5rocks.com/en/tutorials/webperformance/usertiming/

答案 2 :(得分:1)

var startTime = Date.now();

// code ...

console.log("Elapsed time (ms): " + (Date.now() - startTime));

答案 3 :(得分:0)

以下是我到目前为止发现的方法:-

方法1:-

let start = window.performance.now()
/// Your code goes here
let end = window.performance.now()
console.log(`Component Persing Time: ${end - start} ms`);

方法2:-

let start = Date.now()
/// Your code goes here
let end = Date.now()
console.log(`Component Persing Time: ${end - start} ms`);

方法3:-

console.time();
// Your code goes here
console.timeEnd();

以上所有方法都可以继续进行,但结果相同。快乐的编码。 :)