In jQuery, is the selector $(‘[id=foo]’) less efficient than $(‘#foo’)?
Share
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.
short and easy: YES !
long story (still short actually)
uses Sizzle (css query engine) to select the element whereas
directly calls
getElementById.To have a really long story, here we go:
$('[id=foo]')is a synonym for$('*:[id=foo]')which uses the universal selector. That means, it querys ALL nodes within your markup and then looks which of those have theid === foo(which then hopefully will only match one element, IDs = unique). That of course, is costly, pretty costly. And that is why you never ever ever ever should write a selector like this!Always fully qualify this if possible, like
$('span:[id=foo]')