Reputation: 6025
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.
Upvotes: 2
Views: 76
Reputation: 3409
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.
Upvotes: 1