forEach vs. jQuery .each vs. for vs for..in (v8)

Revision 8 of this benchmark created by Matthew on


Description

This test compares speeds from ECMAscript5 (262) Array.prototype.forEach vs jQuery .each method and standard for..in, for methods.

Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
  var arr = [],
      loop = 5000;
  
  while (loop--) {
   arr[loop] = loop;
  }
  
  var fn = function(x) {
   x = x;
  };
  
  var jQfn = function(i, v) {
   v = v;
  };
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
each()
$.each(arr, jQfn);
ready
forEach()
arr.forEach(fn);
ready
for..in
for (var i in arr) {
 fn(arr[i]);
}
ready
for
for (var i = 0; i < arr.length; i++) {
 fn(arr[i]);
}
ready
for, caching length
for (var i = 0, len = arr.length; i < len; i++) {
 fn(arr[i]);
}
ready
assign in loop declaration
for (var i = 0, j; j = arr[i]; i++) {
 fn(j);
}
ready

Revisions

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