JQuery $('#id') selector vs $(document.getElementById('id') (v20)

Revision 20 of this benchmark created on


Description

Fastest way get a jQuery object of a DOM element of which I have the ID. document.getElementById has the advantage of not requiring escaping for special characters.

Preparation HTML

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

Setup

var d = {};
    d.id = function (el) {
      return document.getElementById(el);
    }

Test runner

Ready to run.

Testing in
TestOps/sec
jQuery('#id')
jQuery('#el2').length;
ready
jQuery(document.getElementById('id'))
jQuery(document.getElementById('el2')).length;
ready
No jQuery
document.getElementById('el1').getElementById('el2').length;
ready
wrapped getElementById
d.id('el').length
ready
document.querySelector()
document.querySelector('#el').length
ready

Revisions

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