요약
-반복문은 for문이 제일 빠릅니다.
-for문에서 length를 쓰지 말고 변수로 선언해서 사용합니다.
반복문 코드
const arr = Array(1000000).fill().map((v,i)=> i+1);
console.time('for-type1');
const cnt = arr.length;
for(let i =0; i<cnt;i++){}
console.timeEnd('for-type1');
console.time('for-type2');
for(let i =0; i<arr.length;i++){}
console.timeEnd('for-type2');
console.time('for-type3');
for(var i =0; i<arr.length;i++){}
console.timeEnd('for-type3');
console.time('forEach');
arr.forEach(function(item,index){});
console.timeEnd('forEach');
console.time('for-in');
for(let idex in arr){}
console.timeEnd('for-in');
console.time('for-of');
for(let idex of arr){}
console.timeEnd('for-of');
결과
//for문 변수 선언
for-type1: 1.610107421875 ms
//for문 let 사용
for-type2: 1.657958984375 ms
//for문 var 사용
for-type3: 1.68798828125 ms
forEach: 9.888916015625 ms
for-in: 145.38818359375 ms
for-of: 24.77001953125 ms