[英]Javascript: measure code execution time online
I'm experiencing the need to test the performance differences of some variants of code (native/with plugins).我需要测试某些代码变体(本机/带插件)的性能差异。
Is there an online service, like jsbin, jsfiddle for execution, where I can put the code in, like有没有在线服务,比如 jsbin、jsfiddle 用于执行,我可以把代码放进去,比如
// BEGIN
var bla;
jQuery.map(bla, function(){});
// END
and get execution time?并获得执行时间?
One option is一种选择是
OR或者
//works in chrome and firefox
console.time("myCode"); // 'myCode' is the namespace
//execute your code here
console.timeEnd("myCode");
OR或者
var startTime = window.performance.now();
//execute your code here
console.log(window.performance.now() - startTime);
Using the 'User Timing API' is a modern way of doing this:使用“用户计时 API”是一种现代方法:
http://www.html5rocks.com/en/tutorials/webperformance/usertiming/ http://www.html5rocks.com/en/tutorials/webperformance/usertiming/
var startTime = Date.now();
// code ...
console.log("Elapsed time (ms): " + (Date.now() - startTime));
Below approaches I have found till now:-到目前为止,我发现以下方法:-
Approach 1:-方法一:-
let start = window.performance.now()
/// Your code goes here
let end = window.performance.now()
console.log(`Component Persing Time: ${end - start} ms`);
Approach 2:-方法2:-
let start = Date.now()
/// Your code goes here
let end = Date.now()
console.log(`Component Persing Time: ${end - start} ms`);
Approach 3:-方法三:-
console.time();
// Your code goes here
console.timeEnd();
Any above approches you may proceed but got the same result.任何上述方法您都可以继续,但得到相同的结果。 Happy coding.
快乐编码。 :)
:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.