簡體   English   中英

Javascript:在線測量代碼執行時間

[英]Javascript: measure code execution time online

我需要測試某些代碼變體(本機/帶插件)的性能差異。

有沒有在線服務,比如 jsbin、jsfiddle 用於執行,我可以把代碼放進去,比如

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

並獲得執行時間?

一種選擇是

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

使用“用戶計時 API”是一種現代方法:
http://www.html5rocks.com/en/tutorials/webperformance/usertiming/

var startTime = Date.now();

// code ...

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

到目前為止,我發現以下方法:-

方法一:-

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

方法三:-

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

任何上述方法您都可以繼續,但得到相同的結果。 快樂編碼。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM