For vs ForIn vs ForOf vs ForEach

JavaScript performance comparison

Test case created by borro

Preparation code

<script>
  var array = new Array();
  for (var i = 0; i < 1000; i++) {
    array.push(i);
  }
</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
var dummy = 0;
for (var i = 0; i < array.length; i++) {
  dummy += array[i];
}
pending…
forof
var dummy = 0;
for (let value of array) {
  dummy += value;
}
pending…
forin
var dummy = 0;
for (let key in array) {
  dummy += array[key];
}
pending…
forEach fat arrow
var dummy = 0;
array.forEach(value => {
  dummy += value;
});
pending…
Reduce
var res = array.reduce(function(sum, x) {
  return sum + x;
}, 0);
pending…
forEach
var dummy = 0;
array.forEach(function(value) {
  dummy += value;
});
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