2020-11-12(关于 console.time()和console.timeEnd())

使用console.time() 和 console.timeEnd()可以测试代码运行所耗时间

eg:
let arr = [{a:1},{a:2},{a:3},{a:4},{a:5}];
console.time('for-time');
for(let i=0;i<arr.length;i++){
arr[i].other = Math.random()
}
console.timeEnd('for-time'); //这里需要注意一下:这里面的内容需要和console.time('for-time')里面的内容保持一致哦

console.time('map-time');
arr.map(item => {
item.other = Math.random()
})
console.timeEnd('map-time');

//正确打印显示结果:
for-time 0.033935546875 ms
map-time 0.09375 ms

//ps:如果不保持一致的话,是打印不出时间的哦,而且还会提示: Timer 'xx' does not exist 。

哈哈哈,差点整迷糊了~~~

posted on 2020-11-12 17:10  有匪  阅读(582)  评论(0编辑  收藏  举报

导航