jQuery each2 plugin vs jQuery core .each
method
JavaScript performance comparison
Preparation code
<script src="https://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>
/*!
* jQuery each2 - v0.2 - 8/02/2010
* http://benalman.com/projects/jquery-misc-plugins/
*
* Inspired by James Padolsey's quickEach
* http://gist.github.com/500145
*
* Copyright (c) 2010 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
(function($) {
// Create a placeholder jQuery object with a length of 1. The single item
// is completely arbitrary and will be replaced.
var jq = $([1]);
$.fn.each2 = function( fn ) {
var i = -1;
while (
// Set both the first element AND context property of the placeholder
// jQuery object to the DOM element. When i has been incremented past the
// end, this[++i] will return undefined and abort the while loop.
( jq.context = jq[0] = this[++i] )
// Invoke the callback function in the context of the DOM element,
// passing both the index and the placeholder jQuery object in. Like
// .each, if the callback returns `false`, abort the while loop.
&& fn.call( jq[0], i, jq ) !== false
) {}
// Return the initial jQuery object for chainability.
return this;
};
})(jQuery);
</script>
<script>
(function($) {
$.fn.eachElement = function(callback) {
for (var i = 0; i < this.length; ++i) {
var element = this.eq(i);
callback.call(element, i, element);
}
};
}(jQuery));
</script>
<script>
// Create a whole bunch of elements for iteration.
var elems = $('<div/>').append(Array(1000).join('<span/>')).children();
</script>
Preparation code output
<!-- the plugin -->
<!--script src="http://github.com/cowboy/jquery-misc/raw/master/jquery.ba-each2.js"></script-->
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
Test | Ops/sec | |
---|---|---|
jQuery each2 plugin
|
|
pending… |
jQuery core
.each method
|
|
pending… |
eachElement
|
|
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.
- Revision 1: published "Cowboy" Ben Alman
- Revision 2: published "Cowboy" Ben Alman
- Revision 3: published
- Revision 4: published David
- Revision 5: published David
- Revision 6: published Kyle Simpson and last updated
- Revision 7: published Flatlineato
- Revision 13: published Scott Rycroft
- Revision 15: published Scott Rycroft
- Revision 16: published Jason DiMeo
- Revision 18: published Jason DiMeo
- Revision 22: published creage
- Revision 23: published
- Revision 24: published
- Revision 25: published
- Revision 27: published
- Revision 28: published Grant
- Revision 31: published
- Revision 32: published Yukulélé
- Revision 34: published
- Revision 35: published jquery-instanceof-vs-jquery
- Revision 36: published Doug Gale
- Revision 37: published
- Revision 38: published
- Revision 39: published creage
- Revision 41: published
- Revision 42: published
- Revision 43: published
- Revision 44: published
- Revision 45: published KergeKacsa at Index.hu
- Revision 47: published a
0 Comments