for vs forEach vs for-of

JavaScript performance comparison

Revision 584 of this test case created by Benjamin DANIEL

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  let values = Array.from({length: 10000}, (v, k) => k+1);
  let sum = 0;
  
  let add = function(x) {
      sum += x;
  }

};

Benchmark.prototype.teardown = function() {
  sum = 0;
  values = [];

};
</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
for (let i = 0, l = values.length; i < l; i++) {
    sum += values[i];
}
pending…
forEach
values.forEach((v) => {
    sum += v;
});
pending…
for-of-callback
for (let v of values) {
    add(v);
}
pending…
for-callback
for (let i = 0, l = values.length; i < l; i++) {
    add(values[i]);
}
pending…
forEach-callback
values.forEach((v) => {
    add(v);
});
pending…
for-of
for (let v of values) {
    sum += v;
}
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