I have had this feeling that $('.class:first') runs faster than $('.class'). So anytime I know there only is one .class in the subset, I’ve used it.
Does :first make the query run faster, or is it unnecessary?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It actually depends on the browser,
:firstisn’t a CSS selector, it’s a jQuery filter – so that requires some extra parsing work…where as.classby itself can be handed off to a native browser selector method (e.g.document.querySelectorAll()here).Any of these would actually be faster:
…since they run native code then just take the first entry in that set.