Is selecting by ID-only (or a single identifier) faster than when adding additional identifiers?
For example
$('#element')
vs
$('#container #element')
or even more specific:
$('body div#container div#element')
?
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.
$('#element')should be fastest, followed by$('div'). These map to the native functionsdocument.getElementByIdanddocument.getElementsByTagName. Anything more complex has to go through complex scripting and document searching.On everything but IE6, 7, and 8,
$('.class')also map to the newdocument.getElementsByClassNamefunction as well, but this is slower than the other two and still has to go through Sizzle if the browser doesn’t support it.