Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery find versus context selection

Having the following html snippet

<div class="something">     <p>Some text</p> </div> <div class="somethingElse">     <p>some other text</p> </div> 

I think the following jquery snippets are identical (will have the same result):

$(".something").find("p").css("border", "1px solid red");  $("p", ".something").css("border", "1px solid red"); 

My question is, whether one snippet is better than the other and should be used

like image 896
harpax Avatar asked Feb 26 '10 14:02

harpax


1 Answers

The calls are not identical.

According Brandon Aaron, who apparently worked on jQuery, and also according to the live tests here, the find method is always faster. See results in the screenshot below. Please comment if I am missing something.

With a 10% or greater difference in speed, depending on browser, it definitely seems worth using find.

Further explanation at Brandon's site is here.

Results of performance comparison between jQuery context and jQuery find method

like image 165
BrianFinkel Avatar answered Sep 21 '22 05:09

BrianFinkel