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