for each and regular

JavaScript performance comparison

Test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var arr = [];
  arr.length = 10000000;
  //arr.fill(1);
  for (let i_1 = 0; i_1 < arr.length; i_1++) {
  arr[i_1] = 1;
  }

};
</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 regular
var sum = 0;
for (let i = 0, l = arr.length;  i < l; ++i) {
    let a = arr[i];
    if (a & 1) {
        sum += a;
    }
    else {
        sum -= a;
    }
}
pending…
for regular 2
var sum = 0;
for (let i = 0; i < arr.length; ++i) {
    let a = arr[i];
    if (a & 1) {
        sum += a;
    }
    else {
        sum -= a;
    }
}
pending…
for regular 3
var sum = 0;
for (let i = 0; i < arr.length; i++) {
    let a = arr[i];
    if (a & 1) {
        sum += a;
    }
    else {
        sum -= a;
    }
}
pending…
for each
var sum = 0;
arr.forEach(function(a,i,x){
    if (a & 1) {
        sum += a;
    }
    else {
        sum -= a;
    }
});
pending…
for each 2
var sum = 0;
arr.forEach(function(value){
    let a = value;
    if (a & 1) {
        sum += a;
    }
    else {
        sum -= a;
    }
});
pending…
for each 3
var sum = 0;
arr.forEach(function(a){
    if (a & 1) {
        sum += a;
    }
    else {
        sum -= a;
    }
});
pending…

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

0 Comments