for ,for in, for in,for of,map,while, do while性能测试及排序

tech2023-06-07  100

好好学习 ,天天向上。Are you ready?

代码奉上!!!!!!!!!

let testArr = new Array(10000000).fill('2'); // map console.time('map') testArr.map(item => item) console.timeEnd('map') // forEach console.time('forEach') testArr.forEach(item => { item }) console.timeEnd('forEach') // for console.time('for') for (let i = 0; i < testArr.length; i++) { testArr[i] } console.timeEnd('for') // while console.time('while') let i = 0; while (i < testArr.length) { i++; } console.timeEnd('while') //do while console.time('do while') let s = 0; do { s++ } while (s <= testArr.length) console.timeEnd('do while') // for in console.time('for in') let n = 0 for (n in testArr) { n } console.timeEnd('for in') // for of 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--才是王道!!!
最新回复(0)