JQuery $('#id') selector vs $([id='id']) (v29)

Revision 29 of this benchmark created on


Description

Fastest way get a jQuery object of a DOM element of which I have the ID. # selector has the advantage of using the native getElementById method under the covers while the attribute selector must traverse both the dom and the attributes of the dom element.

Preparation HTML

<div id="el">Some HTML element</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/prototype/1/prototype.js"></script>

<script>
jQuery.noConflict();

var el = document.getElementById('el'), did='el', jid='#el';

function S4() {
  return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
</script>

Setup

el.id = did = "i" + S4();
    jid = '#' + did;
    aid = '[id="' + did +'"]';

Test runner

Ready to run.

Testing in
TestOps/sec
jQuery('#id')
jQuery(jid);
ready
jQuery('[id=id]')
jQuery(aid);
ready

Revisions

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