For loop vs forEach Loop

Edit on GitHub

Javascript array iteration comaprison between for and forEach.

Setup

var arr = [];
  for (var i = 0; i < 1000; i++) {
    arr[i] = 'value' + i;
  }

  function someFn(ix) {
    return ix * 5 + 1 / 3 * 8;
  }

Tests

  • For loop

    for (var i = 0; i < arr.length; i++) {
      someFn(arr[i]);
    }
  • forEach Loop

    arr.forEach(function(a,i){
    someFn(a);
    })