Run details:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36
Chrome 104
Mac OS X 10.15.7
Desktop
2 years ago
Test name Executions per second
for-native 7605.7 Ops/sec
for-with-init 7426.0 Ops/sec
for..of-native 13518.3 Ops/sec
for..of-with-init 13319.0 Ops/sec
map 84485.4 Ops/sec
Script Preparation code:
x
 
   var arr = [];
   for (var i = 0; i < 1000; i++) {
       arr[i] = i;
   }
   function someFn(i) {
       return i * 3 * 8;
   }
Tests:
  • for-native

     
    var newArr = [];
    for (var i = 0, len = arr.length; i < len; i++) {
      newArr.push(someFn(arr[i]));
    }
  • for-with-init

     
    var newArr = new Array(arr.length);
    for (var i = 0, len = arr.length; i < len; i++) {
      newArr.push(someFn(arr[i]));
    }
  • for..of-native

     
    var newArr = [];
    for (const el of arr) newArr.push(someFn(el));
  • for..of-with-init

     
    var newArr = new Array(arr.length);
    for (const el of arr) newArr.push(someFn(el));
  • map

     
    var newArr = arr.map(someFn)