跳到主要内容
CNode

如何快速测试代码的执行效率

社区
Jjifeng发布于 14 年前最后回复 14 年前
275550

要测试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)

I
italkboy#214 年前

学习啦~~

F
fengmk2#114 年前

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

参与回复

登录后即可参与回复。登录