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
Comparison of performance between jQuery's selector and a helper function which can handle crazy ID strings.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<p id="foo">Bar</p>
<p id="foo!foo.foo/foo">Bar Too</p>
function getById(id) {
return document.getElementById(id);
}
function $getById(id) {
return $(document.getElementById(id));
}
var $foo = $('#foo');
// use double-hash to force id-only selection
function get$(el) {
el = el.split('##');
if (el[0] === '') {
return $(document.getElementById(el[1]));
}
return $(el.join(''));
}
function $$(el) {
if (!el || !jQuery) {
return null
}
var $ = jQuery,
$el = el,
id_prefix = /##|#:|id:/,
objType = Object.prototype.toString;
if (!$el.jquery) {
// special case for wrapping a native
// DOM element (fastest)
if (el.nodeType && el.nodeType === 1) {
$el = $(el);
}
// special case for (faster) handling of id
// using a 'special' id-only selector:
// '#:', '##', or 'id:'
// (this will tolerate crazy id strings)
// document.getElementyById('foo.bar/baz') ==
// $$('##foo.bar/baz') == $$('#:foo.bar/baz') == $$('id:foo.bar/baz')
else if (el.search(id_prefix) === 0) {
el = el.replace(id_prefix, '');
$el = $(document.getElementById(el));
}
// jQuery can also wrap a NodeList - right?
else if (['[object HTMLCollection]', '[object NodeList]'].indexOf(objType.call(el) > -1)) {
$el = $(el);
}
// NOW try the standard jQuery selector
else {
$el = $(el);
}
// if there's not a matching DOM element yet,
// then it's PROBABLY just an id string
// without the 'special' prefix.
// (this will also tolerate crazy ids)
if (!$el.length) {
$el = $(document.getElementById(el));
}
// if there's STILL not a matching DOM element
// after trying to find an element with that ID,
// then set $el to null
if (!$el.length) {
$el = null;
}
}
return $el;
}
Ready to run.
Test | Ops/sec | |
---|---|---|
jQuery |
| ready |
native |
| ready |
helper |
| ready |
id (string only) helper |
| ready |
special id string '##' helper |
| ready |
native crazy id |
| ready |
jQuery + native |
| ready |
basic helper |
| ready |
basic jQuery helper |
| ready |
helper with jQuery object |
| ready |
helper with cached jQuery object |
| ready |
helper with native object |
| ready |
get$ helper |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.