jQuery ID selector performance

JavaScript performance comparison

Test case created by Tobias Sjösten

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div id="my-id" class="my-class"></div>
      
<script>
Benchmark.prototype.setup = function() {
  $elem = $('#my-id');

};
</script>

Preparation code output

<div id="my-id" class="my-class"></div>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Saved jQuery object
var x = $elem.length + Math.random();
pending…
ID selector
var x = $('#my-id').length + Math.random();
pending…
ID and tag selector
var x = $('div#my-id').length + Math.random();
pending…
DOM element
var x = $(document.getElementById('my-id')).length + Math.random();
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.

0 Comments