jQuery.fn.each vs. jQuery.fn.quickEach vs. Prototype Array.each usecase only
JavaScript performance comparison
Info
The quickEach method will pass a non-unique jQuery instance to the callback meaning that there will be no need to instantiate a fresh jQuery instance on each iteration. Most of the slow-down inherent in jQuery’s native iterator method (each) is the constant need to have access to jQuery’s methods, and so most developers see constructing multiple instances as no issue… A better approach would be quickEach.
Further added the each implementation from the Prototype library (with a small variation) which yields better results, because when available it will use the browser Array.each implementation.
Preparation code
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
var a = $('<div/>').append(Array(100).join('<a></a>')).find('a');
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;
};
}());
// The Prototype library Array.each implementation
jQuery.fn.prototypeEach = (function() {
var arrayProto = Array.prototype,
slice = arrayProto.slice,
_each = arrayProto.forEach;
return !!_each ? _each : function(iterator, context) {
for (var i = 0, length = this.length >>> 0; i < length; i++) {
if (i in this) iterator.call(context, this[i], i, this);
}
}
})();
// My each2 plugin
(function($) {
var jq = $([]);
$.fn.each2 = function(fn) {
var i = -1,
elem;
while ((elem = jq[0] = this[++i]) && fn.call(elem, i, jq) !== false) {}
return this;
};
})(jQuery);
</script>
Preparation code output
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
.each() |
|
pending… |
.quickEach() |
|
pending… |
.prototypeEach() |
|
pending… |
.each2() |
|
pending… |
for loop |
|
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:
- Revision 1: published by James Padolsey
- Revision 2: published by "Cowboy" Ben Alman
- Revision 3: published by "Cowboy" Ben Alman
- Revision 9: published by NilColor
- Revision 13: published
- Revision 14: published
- Revision 15: published
- Revision 18: published
- Revision 19: published
- Revision 22: published by mhitza
- Revision 27: published
- Revision 28: published by Test
- Revision 29: published
- Revision 30: published by Erwan Legrand
- Revision 31: published
- Revision 32: published
- Revision 33: published
- Revision 34: published by Vaibhav
- Revision 35: published
- Revision 36: published
- Revision 37: published
- Revision 38: published
- Revision 39: published
- Revision 41: published by Jason
- Revision 42: published by Dim
- Revision 47: published by Andy Harman
- Revision 48: published by Andy Harman and last updated
- Revision 50: published by private_face
- Revision 54: published
- Revision 56: published by David Seigle
- Revision 58: published by Andy Harman
- Revision 59: published by David Seigle
- Revision 63: published by Andy Harman
- Revision 64: published by roviury
- Revision 66: published by Andy Harman
- Revision 69: published by Andy Harman and last updated
- Revision 70: published
- Revision 71: published
- Revision 73: published
- Revision 74: published
- Revision 76: published
- Revision 77: published
- Revision 78: published by Jeffrey
- Revision 79: published
- Revision 80: published
- Revision 81: published by nod_ and last updated
- Revision 82: published by Andy Harman
- Revision 83: published by Kirill
- Revision 85: published
0 comments