我如何衡量脚本从头到尾运行所需的时间?
start-timing
//CODE
end-timing
Run Code Online (Sandbox Code Playgroud)
Arn*_*anc 42
编辑:2011年1月,这是最好的解决方案.其他解决方案(例如performance.now()应该首选.
var start = new Date();
// CODE
var time = new Date() - start;
// time is the number of milliseconds it taken to execute the script
Run Code Online (Sandbox Code Playgroud)
您可能还想将其包装在函数中:
function time_my_script(script) {
var start = new Date();
script();
return new Date() - start;
}
// call it like this:
time = time_my_script(function() {
// CODE
});
// or just like this:
time = time_my_script(func);
Run Code Online (Sandbox Code Playgroud)
如果您尝试配置代码,可能需要尝试Firebug扩展,其中包括一个javascript探查器.它有一个很好的用户界面进行性能分析,但它也可以通过其控制台api以编程方式完成:
console.time('timer1');
// CODE
console.timeEnd('timer1'); // this prints times on the console
console.profile('profile1');
// CODE
console.profileEnd('profile1'); // this prints usual profiling informations, per function, etc.
Run Code Online (Sandbox Code Playgroud)