for v each v iterator v Array.forEach

JavaScript performance comparison

Revision 3 of this test case created by Rich

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
  var array1 = new Array();
  for (var i = 0; i < 10000; i++) {
   array1[i] = 0;
  }
  var array2 = new Array();
  for (var i = 0; i < 10000; i++) {
   array2[i] = 0;
  }
  var array3 = new Array();
  for (var i = 0; i < 10000; i++) {
   array3[i] = 0;
  }
  
  function createIterator(x) {
   var i = 0;
  
   return function() {
    return x[i++];
   };
  }
  var iterator = createIterator(array3);
  
  var array4 = new Array();
  for (var i = 0; i < 10000; i++) {
   array4[i] = 0;
  }
</script>
    

Preparation code output

<script> var array1 = new Array(); for (var i = 0; i < 10000; i++) { array1[i] = 0; } var array2 = new Array(); for (var i = 0; i < 10000; i++) { array2[i] = 0; } var array3 = new Array(); for (var i = 0; i < 10000; i++) { array3[i] = 0; } function createIterator(x) { var i = 0; return function() { return x[i++]; }; } var iterator = createIterator(array3); var array4 = new Array(); for (var i = 0; i < 10000; i++) { array4[i] = 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
native
var l = array1.length;
for (var i = 0; i < l; i++) {
 var a = 1 + 2;
}
pending…
jquery
$.each(array2, function(i) {
 var a = 1 + 2;
});
pending…
iterator
var current, current2;
while (current = iterator()) {
 var a = 1 + 2;
}
pending…
Array.forEach
var idx = 0;
Array.forEach(array4, function(ar) {
 var a = 1 + 2;
});
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