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

Revision 62 of this benchmark created by Sumit on


Description

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

Modified preparation code to make the last test case working. (But be carefull this test case will not work if your array contains any "false" values... So, discouraged)

Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
  var arr = [],
      loop = 50;
  
  while (loop--) {
   arr[loop - 1] = 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.