Effect of narrowing by id on Sizzle (jQuery) efficiency

JavaScript performance comparison

Revision 2 of this test case created

Info

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

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<div id="parent">
  <div class="a">
    a
  </div>
  <div class="a">
    a
  </div>
  <div class="a">
    a
  </div>
  <div class="a">
    a
  </div>
  <div class="a">
    a
  </div>
  <div class="a">
    a
  </div>
  <div class="a">
    a
  </div>
  <div class="a">
    a
  </div>
  <div class="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
a
a
a
a
a
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 and id
$('#parent .a').css({
  border: '1px solid green'
});
pending…
Class only
$('.a').css({
  border: '1px solid red'
});
pending…
querySelectorAll w/ id
loop(doc.querySelectorAll('#parent .a'));
pending…
querySelector w/o id
loop(doc.querySelectorAll('.a'));
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