Effect of narrowing by id on Sizzle (jQuery) efficiency

JavaScript performance comparison

Revision 5 of this test case created

Info

See http://stackoverflow.com/questions/1229259/jquery-pitfalls-to-avoid/1229405#1229405

Preparation code

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js">
</script>
<div id="parent">
  <div class="a">
    a
  </div>
  <div id="a">
    a
  </div>
 
</div>
<script>
  var doc = document;

  function loop(ele) {
    for (var i = ele.length; i--;) {
      $(ele[i]).css({
        border: '1px solid pink'
      });
    }
  };
</script>

Preparation code output

a
a

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Class only
$('.a').css({
  border: '1px solid red'
});
pending…
Class and id
$('#a').css({
  border: '1px solid red'
});
pending…
tag
$('a').css({
  border: '1px solid red'
});
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:

1 comment

Will commented :

The tag test appears to be searching the entire DOM of this page, not just the test area, and thus returns a much larger array. This might be affecting the test.

Add a comment