UNPKG

190819-utils

Version:

尚硅谷大前端自定义工具函数库

60 lines (52 loc) 1.24 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>测试各种遍历数组的效率</title> </head> <body> <script> /* 测试: for...in / for / forEach / keys&forEach / while 耗时: for...in | keys&forEach > for | forEach | while */ const arr = new Array(); for (let i = 0; i < 100000; i++) { arr.push(1) } // for..in console.time() for (const key in arr) { let element = arr[key]; } console.timeEnd() //for console.time() for (let index = 0; index < arr.length; index++) { let element2 = arr[index]; } console.timeEnd() // forEach console.time() arr.forEach(item => { let element3 = item; }) console.timeEnd() // keys() + forEach console.time() Object.keys(arr).forEach(index => { let element32 = arr[index] }) console.timeEnd() // while console.time() const length = arr.length let index = 0 while(index++ < length) { let element4 = arr[index] } console.timeEnd() </script> </body> </html>