Is there an efficiency difference between finding by id and finding by class with JavaScript/jquery?
Is one better than the other? If so is it because indexes of ID or Class are made in the DOM somewhere?
Does it end up not mattering much?
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.
Finding by ID is always faster, no matter where (
getElementById(),$('#id'), or even in regular CSS).Since ID’s are unique per page, they’re much faster to find.
In addition, when using
$('#id'), jQuery will map that back to the built-ingetElementById(), which is the fastest way to query the DOM.