jquery find speed vs selector

JavaScript performance comparison

Revision 4 of this test case created by ac

Info

$('body').find('#myDiv').find('img').addClass('test'); vs $('body').find('#myDiv img').addClass('test'); vs $('#myDiv img').addClass('test');

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>

<div id="myDiv">
  <img src="#" />
</div>

<script>
for(var i = 0; i<5000; i++){
$('img').appendTo('#myDiv');
}
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
find every element
$('body').find('#myDiv').find('img').addClass('test');
 
pending…
find
$('body').find('#myDiv img').addClass('test');
 
pending…
direct
$('#myDiv img').addClass('test');
pending…
with find
$('#myDiv').find('img').addClass('test');
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