forEach vs for..of vs reverse index based for loop

JavaScript performance comparison

Revision 4 of this test case created by Barnabás BARTHA

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  const randomStrings = [];
  for (let i = 0; i < 10000; i++) 
  {
      randomStrings.push(Math.random().toString(36).substring(10));
  }
  const randomStringsCopy = [];

};
</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
forEach
randomStrings.forEach(val => randomStringsCopy.push(val));
pending…
for..in
for(let i in randomStrings){
    randomStringsCopy.push(randomStrings[i]);
}
pending…
for..of
for (let randomString of randomStrings){
    randomStringsCopy.push(randomString);
}
pending…
for loop, reverse, pre-decrement
for (let i = randomStrings.length - 1; i >= 0; --i) {
    randomStringsCopy.push(randomStrings[i]);
}
pending…

Revisions

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

0 Comments