getElementById VS jQuery('#id') (v159)

Revision 159 of this benchmark created on


Preparation HTML

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js">
</script>

<script>
var D = function( sel ){
        var dict = {
                '#': function( substr ){ document.getElementById( substr ) },
                '.': function( substr ){ document.getElementsByClassName( substr ) }    
        }
        return dict[sel[0]](sel.substring(1));
};

var Dob = (function(){
return {
                id: function( s ){ document.getElementById( s ) },
                class: function( s ){ document.getElementsByClassName( s ) },
    tag: function( s ){ document.getElementsByTagName( s ) }
}
})();

//TOTALLY CRAZY
String.prototype.idofEl = function(){
return document.getElementById( this );
}

cachedObj = {foo: document.getElementById('foo')};


</script>

<p id="foo">
  Test
</p>
<p id="bar">
  bars</p>

Test runner

Ready to run.

Testing in
TestOps/sec
getElementById
$(document.getElementById('foo'));
ready
jQuery Selector
$('#foo');
ready
Custom Method
function _(id){
    return jQuery(document.getElementById(id));
}
_('foo');
 
ready
$ Custom Method
function _(id){
    return $(document.getElementById(id));
}
_('foo');
 
ready
jQuery Window Object
$('#foo');
$('#bar');
ready
realgetelementbyid
document.getElementById('foo');
ready
typeof
if(typeof (document.getElementById('foo')) === "undefined"){}
ready
Cust. Object using Dictionary
D('#foo');
ready
Cust. Object
Dob.id('foo');
ready
Totally Insane
'foo'.idofEl();
ready
CachedObj
cachedObj['foo'];
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.