7

Slide 30 in Paul Irish's blog mentioned:

$('#container').find('div.robotarm') is faster than $('#container div.robotarm')

Is this true?

2
  • 3
    I'd say opinions count for nothing. Create some benchmarks and run them multiple times (preferably on different browsers) and check the results. That's the only way to get a definitive answer, IMO.
    – Dan Diplo
    Aug 6, 2010 at 8:51
  • forrst.com/posts/… This might help.
    – Kriem
    Apr 8, 2014 at 11:18

3 Answers 3

5

Maybe in an earlier version of jQuery that was the case. However, the expression

$('#container div.robotarm')

is normalized through jQuery into

$('#container').find('div.robotarm')

So the only reason why $('#container div.robotarm') should be slower is because of function call overhead. But, that would really be a trivial difference.

If that call wasn't normalized, sizzle (Resigs css selector engine) would be used to lookup that element (right to left). That of course would be much slower.

1
  • 1
    Can anyone cite some documentation to back this up? Couldn't find anything on a quick google search. Oct 1, 2012 at 11:30
2

Since you asked for opinion, it doesn't matter.

You can always come up with a case where one runs faster than the other in some browser under a certain configuration of the DOM. No need to split hairs.

1

This is only correct when searching by ID.

But when we search by tag name it returns different results in modern browsers where $('div').find('p') is slower than $('div p') because the latter uses querySelector().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.