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

Revision 14 of this benchmark created on


Description

Fastest way get a jQuery object of a DOM element of which I have the ID. Tests if it's faster select by element type then id or just the id.

Preparation HTML

<input class="test1" id="el1">
Some HTML element
</input>
<input class="test" id="el2">
Some HTML element
</input>
<input class="test" id="el3">
Some HTML element
</input>
<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
id
jQuery('#el1');
ready
class
jQuery('.test1');
ready

Revisions

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