jQuery each2 plugin vs jQuery core .each method

JavaScript performance comparison

Test case created by "Cowboy" Ben Alman

Info

Basically, if you're going to do $(this) inside an .each loop, you should consider using the jQuery each2 plugin instead, because it's specifically optimized for this extremely common use case!

Preparation code

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

<!-- the plugin -->
<script src="http://github.com/cowboy/jquery-misc/raw/master/jquery.ba-each2.js"></script>
 
<script>
 // Create a whole bunch of elements for iteration.
 var elems = $('<div/>').append(Array(1000).join('<span/>')).children();
</script>

Preparation code output

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

Testing in unknown unknown
Test Ops/sec
jQuery each2 plugin
elems.each2(function(i, jq) {
 this; // DOM element
 jq; // jQuery object
});
pending…
jQuery core .each method
elems.each(function(i, elem) {
 this; // DOM element
 $(this); // jQuery object
});
pending…

Results

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