jQuery.each vs. alternatives

JavaScript performance comparison

Revision 49 of this test case created

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;

  jQuery.fn.quickEach = (function() {
   var jq = jQuery([1]);
   return function(c) {
    var i = -1,
        el, len = this.length;
    try {
     while (++i < len && (el = jq[0] = this[i]) && c.call(jq, i, el) !== false);
    } catch (e) {
     delete jq[0];
     throw e;
    }
    delete jq[0];
    return this;
   };
  }());
</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 with KeyVal
$.each(a, function(key, val) {
 e = val;
});
pending…
for loop
var len = a.length;
for (var i = 0; i < len; i++) {
 e = $(a[i]);
};
pending…
alternative for loop
for (var i in a) {
 e = $(a[i]);
};
pending…
jQuery.fn.each
b.each(function(i){
 e = $(this);
});
pending…
jQuery.fn.quickEach()
b.quickEach(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