I’ve been really focused on javascript and improving performance for my website. One thing i do often is, create elements dynamically, and access these elements using ids.
Maybe you can help me with some questions.
-
What are the major draw backs for giving every interesting node in the document a unique id?
-
What is the ideal number of ids in a document?
-
Is there a maximum number of ids for a document?
-
In terms of performance, is getting an element by css class slower than that of getting it by id?
Thank you guys for your answers. If you have any additional notes to these questions about dom and accessing them, it would be appreciated.
Thank you.
I know of no real penalty of using “id” values liberally, other than the one annoyance of IE, Safari, and Chrome promoting “id” values to
windowproperties. Good JavaScript code should be able to deal with that, however.Note that:
In modern browsers, lookups by class can be pretty fast, but that’s only because the work of doing it has been submerged into the low-level support code of the browser (possibly supported by more elaborate internal data structures, caches, etc). Now class names are also very important, both for simple semantic markup and for use by client-side code, so I’m not saying that classes are bad or anything. In fact there are times when doing things exclusively by “id” would be fairly stupid, when using classes would introduce simplicity.
edit — as of now (end of 2013) Firefox creates
windowproperties for elements with “id” attributes too. 🙁