jQuery.each vs. alternatives

JavaScript performance comparison

Revision 55 of this test case created

Info

Each comparisons

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>

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
var a = $('*').get(),
    e;

$.each(a, function() {
  e = $(this);
});
pending…
for loop
var a = $('*').get(),
    e, len = a.length;
for (var i = 0; i < len; ++i) {
  e = $(a[i]);
};
pending…
while loop
var a = $('*').get(),
    e, i = a.length - 1;

while (e = a[--i]) {
  $(e)
};
pending…
alternative for loop
var a = $('*').get(),
    e;

for (var i in a) {
  e = $(a[i]);
};
pending…
reverse for
var a = $('*').get(),
    e;

for (var i = a.length; --i;) {
  e = $(a[i]);
}
pending…
While 2
var a = $('*').get(),
    e, i = a.length;

while (--i) {
  e = $(a[i]);
}
pending…
jQuery.fn.each
$('*').each(function(i) {
  e = $(this);
});
pending…
Native foreach
var a = $('*').get(),
    e = a.length;
a.forEach(function (item) {
    e = $(item);
});
pending…
each with reused function
var a = $('*').get(),
    e;

function f() {
  e = $(this);
}

$.each(a, f);
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