jQuery $( 'selector' ) vs custom $( 'selector' )
JavaScript performance comparison
Preparation code
<section>
<div><div><div><div><div id="end" class="end"><div><div><div><div><div><div><div><div>
<article>
<p>
<span id="start" class="start"></span>
</p>
</article>
</div></div></div></div></div></div></div></div></div></div></div></div></div>
</section>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
Benchmark.prototype.setup = function() {
function $2( selector ) {
switch( selector[0] ) {
case '#':
return document.getElementById( selector.substring( 1 ) );
case '.':
return document.getElementsByClassName( selector.substring( 1 ) );
case '<':
var div = document.createElement( 'div' );
div.innerHTML = selector;
return div.childNodes.length == 1 ? div.childNodes[0] : div.childNodes ;
default: //tags
return document.getElementsByTagName( selector.substring( 1 ) );
};
};
};
</script>
Preparation code output
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
jQuery $( 'selector' ) |
|
pending… |
custom $( 'selector' ) |
|
pending… |
You can edit these tests or add even more tests to this page by appending /edit to the URL.
0 comments