如何快速测试代码的执行效率
发布于 12 年前 作者 jifeng 6605 次浏览 最后一次编辑是 8 年前

要测试javascript代码执行效率,当然有很多方法,今天听了清笃 的一个技术分享,发现之前一直忽略了一个最简单快速的方法:

console.time(name)
Creates a new timer under the given name. Call console.timeEnd(name) with the same name to      stop the timer and print the time elapsed..

console.timeEnd(name)
Stops a timer created by a call to console.time(name) and writes the time elapsed.

比如:

console.time('a');
//你要测试效率的代码
for (var i = 0; i < 1000000; i++) {
}
console.timeEnd('a');

执行结果

 a: 3ms
2 回复

如果要做对比的性能测试,benchmark模块会更加省事

回到顶部