Effect of narrowing by id on Sizzle (jQuery) efficiency

JavaScript performance comparison

Test case created by Gergő Tisza

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>
 

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…

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