forEach vs for..of

JavaScript performance comparison

Revision 3 of this test case created by gk0b

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  let arr = [];
  for (let i = 0; i < 10000; i++) {
      arr.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
for (let i = 0; ... )
for (let i = 0; i < arr.length; i++) {
  arrCopy.push(arr[i]);
}
pending…
concat
arrCopy = [].concat(arr)
pending…
for..of
for (let elem of arr) {
    arrCopy.push(elem);
}
pending…
spread
arrCopy = [...arr]
pending…
map
arrCopy = arr.map(val => val);
pending…
forEach
arr.forEach(val => arrCopy.push(val));
pending…

Revisions

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

0 Comments