forEach vs. jQuery .each

JavaScript performance comparison

Revision 2 of this test case created by Joshua Kehn

Info

This test compares speeds from ECMAscript5 (262) Array.prototype.forEach vs jQuery .each method

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
  var arr = [],
      loop = 5000;
 
  while (loop--) {
   arr[loop] = loop;
  }
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
each()
$.each(arr, function(i, v) {
 v = v;
});
pending…
forEach()
arr.forEach(function(v, i) {
 v = v;
});
pending…
each() return
$.each(arr, function(i, v) {
 if (i == 2500) {
  return false;
 }
});
pending…
forEach() return
var found = false;
arr.forEach(function(v, i) {
 if (found) {
  return false;
 }
 if (i == 2500) {
  found = true;
 }
});
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. Here’s a list of current revisions for this page:

0 comments

Add a comment