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/alternative approach would be quickEach
.
Notes:
The callback function is normally slower than each
so the benefit of quickEach
often isn't dramatic.
This version allows nesting of calls to quickEach
(previous versions did not allow 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).
Using finally
instead of catch
seems to slow FireFox down.
<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');
(function() {
var jqx = [],
level = -1;
jQuery.fn.quickEach = function(c) {
var jq = jqx[++level] || (jqx[level] = jQuery([1]));
try {
var i = -1,
len = this.length;
while (++i < len && c.call(jq, i, jq[0] = this[i]) !== false);
} catch (e) {
jq[0] = null;
level--;
throw e;
}
jq[0] = null;
level--;
return this;
}
})();
</script>
var jq5;
Ready to run.
Test | Ops/sec | |
---|---|---|
.each() |
| ready |
.quickEach() |
| ready |
For loop |
| ready |
While loop |
| ready |
Reuse jq instance |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.