Sets: first in setting up jquery selector help?

If I know that there is at most one such object in my DOM, should I always add: first to the selector to speed up the search? I assume that the selection mechanism will stop as soon as it finds one matching element, but I'm not sure if the filter actually slows down.

+7
source share
2 answers

No, because :first not a standard CSS pseudo-class, and using this will result in your selector not being passed to the native querySelectorAll() DOM function, implemented with browser support (unless you use any other jQuery syntax syntax) .

jQuery will take the entire selector and analyze it on its own (using Sizzle, most likely), which is less than what the browser can do.

+6
source

One practice that is almost true; the more pointers you indicate, the more the engine should check.

So, in your case, if you know that he has only one, do not specify it.

But with an expression below, say 100, you won’t even notice it.

0
source

All Articles