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

JavaScript performance comparison

Revision 14 of this test case created

Info

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 code

<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>
<script>
Benchmark.prototype.setup = function() {
    var d = {};
    d.id = function(el) {
      return document.getElementById(el);
    }
};
</script>

Preparation code output

Some HTML element Some HTML element Some HTML element

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
id
jQuery('#el1');
pending…
class
jQuery('.test1');
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:

0 comments

Add a comment