浏览器 | 系统 | 版本 |
---|---|---|
chrome | mac | 72.0.3626.109 |
测试代码
let testArr=new Array(10000000).fill('1');
console.time('map++');
testArr.map(item=>item)
console.timeEnd('map++')
map++: 207.99609375ms
console.time('for')
for(let i=0;i<testArr.length;i++){testArr[i]}
console.timeEnd('for')
for: 98.39501953125ms
console.time('forEach')
testArr.forEach(item=>{item})
console.timeEnd('forEach')
forEach: 131.97900390625ms
简单测试发现还是for循环最快,forEach次之,map最慢。
当然如果直接通过数组里的数据返回数组,还是推荐使用map
网友评论