getElementById VS jQuery('#id') vs custom getElementById

JavaScript performance comparison

Revision 21 of this test case created

Preparation code

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
</script>
<p id="foo">
  Test
</p>
<p id="bar">
  bars</p>

Preparation code output

Test

bars

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
$getElementById
$(document.getElementById('foo'));
pending…
jQuery Selector
$('#foo');
pending…
Custom Method (jQuery)
function _(id){
    return jQuery(document.getElementById(id));
}
_('foo');
 
pending…
Custom Method (getElementById)
function _(id){
    return document.getElementById(id);
}
_('foo');
 
pending…
jQuery Window Object
$('#foo');
$('#bar');
pending…
realgetelementbyid
document.getElementById('foo');
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

1 comment

Jason Miller commented :

Does jQuery's #id selector exit on first match? I was under the impression it was an alias of [id=foo], which would mean it cannot terminate until it has traversed the whole document.

Add a comment