I have a page with countless thousands of elements. Will the browser work more efficiently (for example, getElementById) if I keep the number of elements with IDs to a minimum, or does it make no difference?
Thanks.
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.
Typically the DOM is loaded into a specialized tree-like data structure, and trees are designed for fast lookups. So once the page is in memory, all the searches will be blazing fast. The loading/parsing would be faster if you had a smaller file size, i.e. no IDs=less chars=less
Bytes. But once it’s all loaded into memory, it shouldn’t matter much.
It would also depend on the kind of algorithm that the browser’s JavaScript engine implements for the getElementById() method. I’m sure Chrome does it differently from IE or FF and vise versa. I wouldn’t worry about this part since you can’t control how each vendor implements their browser.
The main point is, try and keep the file size smaller. Favor short node names and IDs over long ones because, after all, string matching is occurring at some step in the parsing/searching process.