forEach vs loop

JavaScript performance comparison

Revision 163 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var arr = [];
  for (var i = 0; i < 100000; i++) arr[i] = i;
  var sum = 0;
  var fn = function(v) {
    sum += v;
  sum += 2 * v;
  sum += 3 * v;
  sum += 4 * v;
  sum += 5 * v;
  sum += 6 * v;
  sum += 7 * v;
  
  }

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
for with anonymous every time
for (var i = 0, l = arr.length; i < l; i++) {
var func = () => {
var v = arr[i];  
sum += v;
sum += 2 * v;
sum += 3 * v;
sum += 4 * v;
sum += 5 * v;
sum += 6 * v;
sum += 7 * v;
};
func();
}
pending…
loop
for (var i = 0, l = arr.length; i < l; i++) {
var v = arr[i];  
  sum += v;
sum += 2 * v;
sum += 3 * v;
sum += 4 * v;
sum += 5 * v;
sum += 6 * v;
sum += 7 * v;
}
pending…
forEach with anonymous func
arr.forEach((v) => {
  sum += v;
sum += 2 * v;
sum += 3 * v;
sum += 4 * v;
sum += 5 * v;
sum += 6 * v;
sum += 7 * v;

});
pending…
forEach
arr.forEach(fn);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL.

0 Comments