Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will jQuery search for the ID before filtering other parameters in the selector?

This question is related to performance.

If I use a selector like the following

$('#myID a') // Does this find #myID and filter by a?

Or should I write the statement like this?

$('#myID').find('a')

I'm not sure if jQuery is smart enough to execute this statement using the ID first or if it operates exactly like CSS and reads right to left. It's not such a big deal using tags but when you run something like

$('#myID .myClass')

It makes a HUGE difference in performance.

like image 687
THEtheChad Avatar asked Oct 13 '11 17:10

THEtheChad


People also ask

How does jQuery filter work?

The filter() method returns elements that match a certain criteria. This method lets you specify a criteria. Elements that do not match the criteria are removed from the selection, and those that match will be returned. This method is often used to narrow down the search for an element in a group of selected elements.

Which of the following is a method of selecting an element using its ID in jQuery?

The #id Selector The jQuery #id selector uses the id attribute of an HTML tag to find the specific element. An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.

What does jQuery find return?

jQuery find() Method The find() method returns descendant elements of the selected element. A descendant is a child, grandchild, great-grandchild, and so on. The DOM tree: This method traverse downwards along descendants of DOM elements, all the way down to the last descendant.

Which type of parameter can jQuery filter function take?

The jQuery filter() method can take the selector or a function as its argument to filters the set of matched elements based on a specific criteria.


2 Answers

From a NetTuts article: http://net.tutsplus.com/tutorials/javascript-ajax/quick-tip-think-right-to-left-with-jquery/

As an example, if Sizzle comes across a selector like $('#box p'), it’s true that it works right-to-left, but there’s also a quick regex optimization that will first determine whether the first section of the selector is an id. If so, it’ll use that as the context, when searching for the paragraph tags.

Relevant comment from SizzleJS:

// Take a shortcut and set the context if the root selector is an ID
// (but not if it'll be faster if the inner selector is an ID)
like image 121
Dennis Avatar answered Sep 18 '22 18:09

Dennis


When an Id is in the selector. jQuery will first execute document.getElementById then begin filtering for child elements.

basically this is why it is never a great idea to use just attribute or class selectors $('.someclass') or $('[name=myname]') without being more specific. Because it causes the code to traverse the DOM and look at every element to find that class.

By just adding a tagname to the same selector $('div.someclass') or $('div.[name=myname]') you improve efficiency becuase it will first run. document.getElementsByTagName narrowing the number of elements to search.

like image 22
John Hartsock Avatar answered Sep 16 '22 18:09

John Hartsock