forEach vs for..of vs reverse index based for loop (v4)

Revision 4 of this benchmark created by Barnabás BARTHA on


Setup

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

Test runner

Ready to run.

Testing in
TestOps/sec
forEach
randomStrings.forEach(val => randomStringsCopy.push(val));
ready
for..in
for(let i in randomStrings){
    randomStringsCopy.push(randomStrings[i]);
}
ready
for..of
for (let randomString of randomStrings){
    randomStringsCopy.push(randomString);
}
ready
for loop, reverse, pre-decrement
for (let i = randomStrings.length - 1; i >= 0; --i) {
    randomStringsCopy.push(randomStrings[i]);
}
ready

Revisions

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

  • Revision 4: published by Barnabás BARTHA on