jQuery.each vs. alternatives

JavaScript performance comparison

Revision 53 of this test case created by Addy Osmani

Info

Each comparisons

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
  var b = $('*'), a = b.get(),
      e;
</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
jQuery.each
$.each(a, function() {
 e = $(this);
});
pending…
for loop
var len = a.length;
for (var i = 0; i < len; i++) {
 e = $(a[i]);
};
pending…
while loop
var i = a.length - 1;

while (e = a[i--]) {
 $(e)
};
pending…
alternative for loop
for (var i in a) {
 e = $(a[i]);
};
pending…
reverse for
for (var i = a.length; i--;) {
 e = $(a[i]);
}
pending…
While 2
var i = a.length;
while (i--) {
 e = $(a[i]);
}
pending…
jQuery.fn.each
a.each(function(i){
 e = $(this);
});
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