for of vs forEach vs for vs for cached

Edit on GitHub

Setup

var expensiveComputation = n => Math.pow(Math.sqrt(n) * Math.sqrt(n), 20)
var data = []; for (let t = 0; t < 100; ++t) data.push(t);

Tests

  • for of

    for (let n of data)
      expensiveComputation(n);
  • forEach

    data.forEach(n => {
      expensiveComputation(n);
    })
  • for

    for (let t = 0; t < data.length; ++t)
      expensiveComputation(data[t]);
  • for cached

    for (let t = 0, T = data.length; t < T; ++t)
      expensiveComputation(data[t]);