Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$('#id tag') vs. $('#id').find('tag') - which is preferable?

I want to know which option is better, particularly in terms of their speed:

$('#id tag')...

or

$('#id').find('tag')...

Also, would the same answer apply if you change id and/or tag to, say, a class or something like input:checked?

For example, which is better: $('#id input:checked')... or $('#id').find('input:checked');?

like image 522
Nick Avatar asked Jul 16 '12 10:07

Nick


People also ask

What does $(' body ') mean in jQuery?

$(document. body) is using the global reference document to get a reference to the body , whereas $('body') is a selector in which jQuery will get the reference to the <body> element on the document .

What are selectors?

A selector is a chain of one or more simple selectors separated by combinators. Combinators are: white space, ">", and "+". White space may appear between a combinator and the simple selectors around it. The elements of the document tree that match a selector are called subjects of the selector.

What is P jQuery?

jQuery Selector Syntax For example $('p') selects all paragraphs <p> in the document. The #id Selector. Represents a HTML element available with the given ID in the DOM.

What does $( div p select?

Description. "$("div p")" Selects all elements matched by <div> that contain an element matched by <p>.


2 Answers

You can base your decision on 3 things:

Readability

This is not much of a difference with your two given selectors. For my part, I prefer the $('#id').find('inner') syntax because it describes more accurately what it is actually doing.

Reusability

If you have other parts of your code use the same id selector (or something in its context), you can cache the selector and reuse it. I myself find it harder to refactor code that has been written like this $('#id inner'), because you have to decode the css selector first before you can move on and find possible improvements.

Imagine these two cases with not much complexity

$('#hello .class_name tag').doThis();
$('#hello .other_name input').doThat();

And the other case

$('#hello').find('.class_name tag').doThis();
$('#hello').find('.other_name input').doThat();

I think the second example screams at you «Cache the id selector», and the first does not.

Speed

Performance is always a good point, and in this case, the id selector with the find does the better job in most browsers, because it establishes the context first and can apply the descending selector to a smaller subset of elements.

Here's a good performance test, if you want to know more about context-vs subset selectors performance in jQuery. Subsets of ids generally rule. Of course you can get different results, but in most cases, they do.

So, 3 to 0 for subset selectors from my point of view.

like image 59
Beat Richartz Avatar answered Sep 18 '22 13:09

Beat Richartz


Here's the test case HTML where I look for all span elements under #i element:

<div id="i">
  <span>testL1_1</span>
  <span>testL1_2</span>
  <div>
    <span>testL2_1</span>
  </div>
  <ul>
    <li>
      <span>testL3_1</span>
    </li>
  </ul>
  <div>
    <div>
      <div>
        <div>
          <span>testL5_1</span>
        </div>
      </div>
    </div>
  </div>
</div>

Testing these three jQuery selectors:

$("#i span");         // much slower
$("#i").find("span"); // FASTEST
$("span", "#i");      // second fastest

http://jsperf.com/jquery-sub-element-selection

I've run it on Google Chrome and Firefox and it seems that .find() is the fastest closely followed by the third case and much slower first one.

jQuery selector performance

like image 33
Robert Koritnik Avatar answered Sep 21 '22 13:09

Robert Koritnik