for vs. jQuery.each vs. forEach

JavaScript performance comparison

Revision 2 of this test case created by Martijn

Preparation code

<script src='//code.jquery.com/jquery-latest.min.js'></script>
<script>
  var arr = ['a', 'b', 'c'];
</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
for loop
for (var i = 0; i < arr.length; i++) {
 i + ': ' + arr[i];
}
pending…
jQuery.each
$.each(arr, function(i, v) {
 i + ': ' + v;
});
pending…
forEach
arr.forEach(function(v, i) {
 i + ': ' + v;
});
pending…
for loop, cached length
for (var i = 0, l = arr.length; i < l; i++) {
 i + ': ' + arr[i];
}
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