forEach vs for..of

JavaScript performance comparison

Revision 2 of this test case created by gk0b

Preparation code


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

};
</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
reduce
randomStrings.reduce(
  (arrCopy, v) => {
    arrCopy.push(v)
    return arrCopy
  },
  arrCopy
)
pending…
for..of
for (let randomString of randomStrings) {
    randomStringsCopy.push(randomString)
}
pending…
map
arrCopy = randomStrings.map(v => v)
pending…
concat
arrCopy = [].concat(randomStrings)
pending…
forEach
randomStrings.forEach((val) => randomStringsCopy.push(val))
pending…
spread op
arrCopy = [...randomStrings]
pending…

Revisions

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

0 Comments