Array iteration and assignment performance test

JavaScript performance comparison

Revision 86 of this test case created by Keith

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var arr = [];
  for (var i = 0; i < 100000; i++) arr[i] = i;
  var num2 = 0;
  
  function addToSum(num){
     num2 = num;
  }

};

Benchmark.prototype.teardown = function() {
  num2 = 0;

};
</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
for (var i =0; i < arr.length; i++) {
  num2 = arr[i];
}
pending…
forEach
arr.forEach(addToSum);
pending…
forIn
var num = 0;
for (num in arr){
 num2 += num;
}
pending…
forOf
var num = 0;
for (num of arr){
 num2 = num;
}
pending…
for (cached length)
for (var i = 0, l = arr.length; i < l; i++) {
  num2 = arr[i];
}
pending…
for (cached length) (!=)
for (var i = 0, l = arr.length; i != l; i++) {
  num2 = arr[i];
}
pending…
while
var arrLength = arr.length;
while(arrLength--) {
  num2 = arr[arrLength];
}
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