好好学习 ,天天向上。Are you ready?
代码奉上!!!!!!!!!
let testArr
= new Array(10000000).fill('2');
console
.time('map')
testArr
.map(item
=> item
)
console
.timeEnd('map')
console
.time('forEach')
testArr
.forEach(item
=> { item
})
console
.timeEnd('forEach')
console
.time('for')
for (let i
= 0; i
< testArr
.length
; i
++) { testArr
[i
] }
console
.timeEnd('for')
console
.time('while')
let i
= 0;
while (i
< testArr
.length
) {
i
++;
}
console
.timeEnd('while')
console
.time('do while')
let s
= 0;
do {
s
++
}
while (s
<= testArr
.length
)
console
.timeEnd('do while')
console
.time('for in')
let n
= 0
for (n
in testArr
) {
n
}
console
.timeEnd('for in')
console
.time('for of')
for (let value
of testArr
) {
value
}
console
.timeEnd('for of')
本地测试
map
: 614.01708984375ms
forEach
: 482.278076171875ms
for: 28.594970703125ms
while: 65.105224609375ms
do while: 129.32373046875ms
for in: 8779.322265625ms
for of: 346.7978515625ms
由此可见
性能可排序为
for>while>do while>for of>forEach
>map
>for in
for循环性能最强 以此类推
for in最慢
for循环中
for-- 比
for++的性能要好 so
: for--才是王道!!!
转载请注明原文地址:https://tech.qufami.com/read-10828.html