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
Fast alternative to $.fn.each(func). Most usage of fn.each immediately evaluates $(this) - which is really expensive. This version changes this
so that it already points to a single-element jQuery object.
Notes:
The callback function is normally much slower than each
so the benefit of quickerEach
often isn't dramatic. A simple for-loop may be more suitable.
Your callback function MUST NOT call quickEach
(i.e. nesting).
Your callback function MUST NOT store the value of this
to a closure variable (I accidentally did once - and it took me over an hour to track the problem down).
Don't be tempted to replace the jQuery each
method with this.
My previous implementation (48) was bobbins because modern JS engines use short-circuiting to avoid doing work ... and there was a bug that you could drive a bus through.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script>
var a = $('<div/>').append(Array(100).join('<a></a>')).children();
// Modified quickEach plugin
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;
};
}());
// James Padolsey's quickEach plugin
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>
Ready to run.
Test | Ops/sec | |
---|---|---|
.each() |
| ready |
.quickEach() |
| ready |
.quickerEach() |
| ready |
For loop |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.