What is fastest children() or find() in jQuery?

JqueryJquery Selectors

Jquery Problem Overview


To select a child node in jQuery one can use children() but also find().

For example:

$(this).children('.foo');

gives the same result as:

$(this).find('.foo');

Now, which option is fastest or preferred and why?

Jquery Solutions


Solution 1 - Jquery

children() only looks at the immediate children of the node, while find() traverses the entire DOM below the node, so children() should be faster given equivalent implementations. However, find() uses native browser methods, while children() uses JavaScript interpreted in the browser. In my experiments there isn't much performance difference in typical cases.

Which to use depends on whether you only want to consider the immediate descendants or all nodes below this one in the DOM, i.e., choose the appropriate method based on the results you desire, not the speed of the method. If performance is truly an issue, then experiment to find the best solution and use that (or see some of the benchmarks in the other answers here).

Solution 2 - Jquery

This jsPerf test suggests that find() is faster. I created a more thorough test, and it still looks as though find() outperforms children().

Update: As per tvanfosson's comment, I created another test case with 16 levels of nesting. find() is only slower when finding all possible divs, but find() still outperforms children() when selecting the first level of divs.

children() begins to outperform find() when there are over 100 levels of nesting and around 4000+ divs for find() to traverse. It's a rudimentary test case, but I still think that find() is faster than children() in most cases.

I stepped through the jQuery code in Chrome Developer Tools and noticed that children() internally makes calls to sibling(), filter(), and goes through a few more regexes than find() does.

find() and children() fulfill different needs, but in the cases where find() and children() would output the same result, I would recommend using find().

Solution 3 - Jquery

Here is a link that has a performance test you can run. find() is actually about 2 times faster than children().

Chrome on OSX10.7.6

Solution 4 - Jquery

Those won't necessarily give the same result: find() will get you any descendant node, whereas children() will only get you immediate children that match.

At one point, find() was a lot slower since it had to search for every descendant node that could be a match, and not just immediate children. However, this is no longer true; find() is much quicker due to using native browser methods.

Solution 5 - Jquery

None of the other answers dealt with the case of using .children() or .find(">") to only search for immediate children of a parent element. So, I created a jsPerf test to find out, using three different ways to distinguish children.

As it happens, even when using the extra ">" selector, .find() is still a lot faster than .children(); on my system, 10x so.

So, from my perspective, there does not appear to be much reason to use the filtering mechanism of .children() at all.

Solution 6 - Jquery

> Both find() and children() methods are used to filter the child of the matched elements, except the former is travels any level down, the latter is travels a single level down.

To simplify:

  1. find() – search through the matched elements’ child, grandchild, great-grandchild... all levels down.
  2. children() – search through the matched elements’ child only (single level down).

Solution 7 - Jquery

I'm sorry but my own experience will not match with most of the answers here so I think it's interesting to share it here and suggest people to do their own tests for their own usage context.

I have just gained approximatively 40% performance by replacing in several places of my script $(...).find() by $(...).children().

The actual nesting of items was really low (max 3 levels), and despite this children() is clearly much more efficient.

Both were called thousands of times to retrieve informations in a grid (made of thousands of

). The purpose is to mainly to apply some styling to cells / columns / rows depending of several things in the cell content.

With Firefox, before to replace find(), load time for the grid was between 2100 and 2400ms. After the change with children(), it was reduced to something between 1300 and 1500ms (so between 30% and 40% less).

However, with Chrome, it's not so convincing unfortunately : Chrome is much slower (8s) to process exactly the same thing, and I don't see any obvious performance difference before and after this change with it.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbartView Question on Stackoverflow
Solution 1 - JquerytvanfossonView Answer on Stackoverflow
Solution 2 - JqueryJR.View Answer on Stackoverflow
Solution 3 - JquerymactiveView Answer on Stackoverflow
Solution 4 - JqueryJohn FeminellaView Answer on Stackoverflow
Solution 5 - JqueryCraig WalkerView Answer on Stackoverflow
Solution 6 - JqueryNaresh KumarView Answer on Stackoverflow
Solution 7 - JqueryAFractView Answer on Stackoverflow