50

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

5
  • Why don't you use $(".something > p")? That's pure CSS. Commented Feb 26, 2010 at 14:39
  • 1
    @Xr: It would be rather $(".something p"). Commented Feb 26, 2010 at 14:52
  • 5
    $(".something p") is actually the slowest method available because, apparently, jQuery will search from right to left, finding first all p elements in the document, then filtering them for the .something ancestor. $(".something").find("p") is the fastest, as per my answer below. Commented Jan 28, 2012 at 15:34
  • @Xr. jQuery is still a javascript, nothing more. This sintaxis just looks like css, there is no magic behind it that uses native css mechanics inside javascript, as i understand. Commented Apr 13, 2015 at 15:24
  • @MaxYari Indeed. But I think it's useful to keep one syntax rather than multiply the choices. For your information, JQuery is based on a selector library named Sizzle: sizzlejs.com Commented Apr 21, 2015 at 8:10

4 Answers 4

66

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

Sign up to request clarification or add additional context in comments.

3 Comments

I added a test for using the context the "right way" according to the above-mentioned article - by supplying a DOM Node for the second argument. Ironically, it's even slower: jsperf.com/jquery-find-vs-context-2/3
your chart show the opposite from what you said. the red line is the context and it seems to be faster than the blue line(the find() method) Am i missing somthing?
sorry,my confusion...at first glance it seems the opposite, but the x axis is: operations per second - so after undestanding that it makes sense
22

Both calls are identical. The latter call is translated into the former one. If you want to omit the translation step, use the former one.

6 Comments

Might be obvious, but I am uncertain: Is this the same, when the outer selector is already a jQuery object? Like $sth.find('p') is slightly faster than $('p', $sth.)?
Live tests posted in my answer seem to demonstrate that the find method is always faster. Am I missing something?
@BrianFinkel $(selector, context) is ‘translated’ into $(context).find(selector) internally. That translation takes the additional time.
Here is the relevant part of the jQuery (1.10.2) source, for the curious: github.com/jquery/jquery/blob/1.10.2/src/core.js#L175-L183
These calls are not the same... please see tests below
|
11

I can think of one use case where using the context form might be preferable - in the case where the context is contained in a variable which might be null.

For example:

// Only affect matching items that are descendants of '#parent' do_something( $( '#parent' ) ); // Affect all matching items do_something(); function do_something( $parent_element ){ $( '.child', $parent_element ).each( function(){ } ); } 

The second time do_something() is called, if we had used $parent_element.find() it would have failed, whereas in this example, if $parent_element is undefined or empty the context is null, thus: the entire document.

Admittedly this is an edge case, but it just came up in something I was working on, so thought I'd put it here for posterity.

Comments

7

Find is better, by 40%:

http://jsperf.com/jquery-find-vs-context-2/13

Note the difference:

$myDiv = $("myDiv") myDiv = "#myDiv" 

when passing $myDiv, the jQuery element as the context, its about 10% slower than $.find. when passing in #myDiv, the jQuery selector as the context, its 40% slower than $.find.

$.find > context.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.