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
Testing the performance of the native querySelectorAll method against a custom solution that employs getElementById, getElementsByTagName, and getElementsByClassName for simple selectors to improve performance.
<script>
(function(win){
'use strict';
var doc = win.document, simpleRe = /^(#?[\w-]+|\.[\w-.]+)$/, periodRe = /\./g, slice = [].slice;
win.query = function(selector, context){
context = context || doc;
if(simpleRe.test(selector)){
switch(selector.charAt(0)){
case '#':
return context.getElementById(selector.substr(1));
case '.':
return slice.call(context.getElementsByClassName(selector.substr(1).replace(periodRe, ' ')));
default:
return slice.call(context.getElementsByTagName(selector));
}
}
return slice.call(context.querySelectorAll(selector));
};
})(this);
</script>
<div id="foo" class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
Ready to run.
Test | Ops/sec | |
---|---|---|
querySelectorAll |
| ready |
query |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.