我想测试我的js代码的性能,我正在寻找perf
John Resig 在他的书中的函数 "Secrets of Javascript Ninja" .有谁知道如何创建这样的函数或简单函数(与 John 的 perf
相同)而不是使用测试库?
他的函数是这样工作的:
perf('my test', function(){
// code to test for performance
})
我无法在谷歌或图书代码中找到它。
最佳答案
我没有这本书,也不知道这里到底是什么意思,但是类似于 python 的 timeit 可能看起来像这样:
perf = function(fn) {
var times = 1000;
if (parseInt(arguments[0]) > 0) {
times = parseInt(arguments[0]);
fn = arguments[1];
}
var d = new Date();
while (times--) fn();
return new Date() - d;
}
用法:perf(myFunc) 或 perf(5000, myFunc)
关于javascript - John Resig 的 perf 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9060416/