$.each - object supporting length key

JavaScript performance comparison

Revision 6 of this test case created by Jordan Boesch

Preparation code

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

<div>asdasdasdads</div>
<div>asdasdasdads</div>
<div>asdasdasdads</div>
<div>asdasdasdads</div>
<script>
  var divs = jQuery('div');
  jQuery.newEach = function(object, callback, args) {
 
   var name, i = 0,
       length = object.length,
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       // The line below was the only lined I changed
       isObj = !(object instanceof jQuery || length !== undefined && typeof length === "number" && ((length > 0 && object[0] && object[length - 1]) || jQuery.isArray(object))) || jQuery.isFunction(object);
 
   if (args) {
    if (isObj) {
     for (name in object) {
      if (callback.apply(object[name], args) === false) {
       break;
      }
     }
    } else {
     for (; i < length;) {
      if (callback.apply(object[i++], args) === false) {
       break;
      }
     }
    }
 
    // A special, fast, case for the most common use of each
   } else {
    if (isObj) {
     for (name in object) {
      if (callback.call(object[name], name, object[name]) === false) {
       break;
      }
     }
    } else {
     for (; i < length;) {
      if (callback.call(object[i], i, object[i++]) === false) {
       break;
      }
     }
    }
   }
 
   return object;
 
  }
 
  // Gildas modification
  jQuery.newEach2 = function(object, callback, args) {
 
   var name, i = 0,
       length = object.length,
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       // line added
       o,
       
       
       
       
       
       
       
       
       
       
       
       // The line below was the only lined Jordan changed
       isObj = !(object instanceof jQuery || length !== undefined && typeof length === "number" && ((length > 0 && object[0] && object[length - 1]) || jQuery.isArray(object)));
 
   if (args) {
    if (isObj) {
     for (name in object) {
      if (callback.apply(object[name], args) === false) {
       break;
      }
     }
    } else {
     for (; i < length;) {
      if (callback.apply(object[i++], args) === false) {
       break;
      }
     }
    }
 
    // A special, fast, case for the most common use of each
   } else {
    if (isObj) {
     for (name in object) {
      // line added
      o = object[name];
      // line changed
      if (callback.call(o, name, o) === false) {
       break;
      }
     }
    } else {
     for (; i < length; i++) {
      // line added
      o = object[i];
      // line changed
      if (callback.call(o, i, o) === false) {
       break;
      }
     }
    }
   }
 
   return object;
 
  }
</script>

Preparation code output

asdasdasdads
asdasdasdads
asdasdasdads
asdasdasdads

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Old jQuery.each (raw obj)
jQuery.each({
 a: '1',
 b: '2',
 c: '3',
 d: '4'
}, function() {

});
pending…
New jQuery.each (raw obj)
jQuery.newEach({
 a: '1',
 b: '2',
 c: '3',
 d: '4'
}, function() {

});
pending…
Old jQuery.each (collection)
jQuery.each(divs, function() {

});
pending…
New jQuery.each (collection)
jQuery.newEach(divs, function() {

});
pending…
Old jQuery.each (normal array)
jQuery.each([1, 2, 3, 23423, 234234, 443], function() {

});
pending…
New jQuery.each (normal array)
jQuery.newEach([1, 2, 3, 23423, 234234, 443], function() {

});
pending…
Old jQuery.each (function)
var f = function() {};
f.foo = "bar";
jQuery.each(f, function(i) {
 f[i] = 'baz';
});
pending…
New jQuery.each (function)
var f = function() {};
f.foo = "bar";
jQuery.newEach(f, function(i) {
 f[i] = 'baz';
});
pending…
jQuery.newEach2(raw obj)
jQuery.newEach2({
 a: '1',
 b: '2',
 c: '3',
 d: '4'
}, function() {

});
pending…
jQuery.newEach2(collection)
jQuery.newEach2(divs, function() {

});
pending…
jQuery.newEach2 (normal array)
jQuery.newEach2([1, 2, 3, 23423, 234234, 443], function() {

});
pending…
jQuery.newEach2 (function)
var f = function() {};
f.foo = "bar";
jQuery.newEach2(f, function(i) {
 f[i] = 'baz';
});
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