jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
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
.
Added bug fix to roviury's version (it was previous faster because it wasn't doing any work).
Added James Padolsey's original quickEach for comparison.
Added "addClass" to body of loops so that JS runtime can't get fancy ideas about optimising the entire body away.
Added quickerEach for comparison.
Added my previous simple while-loops that demonstrate that quickEach should really not be required.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script>
var a = $('<div/>').append(Array(100).join('<a></a>')).find('a');
//Original version by James Padolsey.
jQuery.fn.origQuickEach = (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;
};
}());
//Updated version supplied by roviury.
jQuery.fn.updatedQuickEach = (function(jq) {
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) {
throw e;
} finally {
delete jq[0];
}
return this;
};
})(jQuery([1])); //Bug-fix: Previously this was just jQuery().
jQuery.fn.quickerEach = (function() {
var jq = jQuery([1]);
return function(c) {
var i = -1,
el;
try {
while (el = jq[0] = this[++i] && c.call(jq, i, el) !== false);
} catch (e) {
jq[0] = 1;
throw e;
}
jq[0] = 1;
return this;
};
}());
</script>
Ready to run.
Test | Ops/sec | |
---|---|---|
.each |
| ready |
Original .quickEach |
| ready |
Updated .quickEach |
| ready |
quickerEach |
| ready |
while loop |
| ready |
while without body |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.