What is the time complexity of finding HTML DOM

Assuming there are no crazy optimizations (I'm looking at you in Chrome).

I am talking about an unprocessed, otherwise, non-broken-non-fix, i.e. v6 javascript, cost.

Bottom line:

document.getElementById() 

Versus:

 document.getElementsByTagName('div') lookup. 
+7
source share
1 answer

getElementById can be considered O(1) in a modern browser because the hash table is an ideal data structure for displaying id => elements.

Without any optimizations, any simple query - be it a css selector, identifier search, class or tag name search - is no worse than O(n) , since one iteration over all elements is always enough.

However, in a good browser, I expect it to have a tag element mapping =>, so getElementsByTagName will also be O(1) .

+9
source

All Articles