测试缓存时间问题console.time

本文介绍了一种使用JavaScript内置方法console.time及其配套方法console.timeEnd来记录代码执行时间的有效方式。这种方法可以帮助开发者评估不同代码片段的运行效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 启动计时器
console.time('testForEach');

// (写一些测试用代码)

// 停止计时,输出时间
console.timeEnd('testForEach');

转载于:https://www.cnblogs.com/haonanya/p/9661418.html

方法一 this.largeObjectArray = this.createLargeObjectArray(); const newWatch = (callback) =>{ const largeObjectArray = this.largeObjectArray; let lastLogTime= 0; const tablePrintTime = this.calculateTime(() => { console.table(largeObjectArray); }); const printLogTime = this.calculateTime(() => { console.log(largeObjectArray); }); if (tablePrintTime > 0 && printLogTime > 0) { if (tablePrintTime > printLogTime * 10) { console.log('时间对比', tablePrintTime, printLogTime); callback(true); return; } lastLogTime = printLogTime; } callback(false ); } 方法二 const newWatch = (callback) =>{ const largeObjectArray = this.createLargeObjectArray(); let lastLogTime= 0; const tablePrintTime = this.calculateTime(() => { console.table(largeObjectArray); }); const printLogTime = this.calculateTime(() => { console.log(largeObjectArray); }); if (tablePrintTime > 0 && printLogTime > 0) { if (tablePrintTime > printLogTime * 10) { console.log('时间对比', tablePrintTime, printLogTime); callback(true); return; } lastLogTime = printLogTime; } callback(false ); } 共同函数createLargeObject,createLargeObjectArray,calculateTime // 创建大对象数组 createLargeObject() { if (!this._largeObject) { this._largeObject = Array.from({length: 500}, (_, i) => ({ key: i, value: `${i}` }) ); } return this._largeObject; } // 创建大对象数组 createLargeObjectArray() { return Array(50).fill(this.createLargeObject()); } // 计算打印执行时间 calculateTime(func) { const start = performance.now(); func() return performance.now() - start; } 解释方法一和方法二谁更完美
03-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值