for-of-vs-for-loop-vsforEach

JavaScript performance comparison

Revision 15 of this test case created by Richard Connamacher

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  this.testArray = "x".repeat(1000).split("").map(function(_, index) {
    return index;
  })

};
</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 of
var total = 0,
  testArray = this.testArray;

for (var value of testArray) {
  total += value;
}
pending…
traditional for loop
var total = 0,
  testArray = this.testArray;

for (var i = 0, length = testArray.length; i < length; i++) {
  total += testArray[i];
}
pending…
forEach
var total = 0,
  testArray = this.testArray;

testArray.forEach(function(value) {
  total += value;
});
pending…
for in
var total = 0,
  testArray = this.testArray;

for (var i in testArray) {
  total += testArray[i];
}
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