Javascript getElementById lookups - hash map or recursive tree traversal?

前端 未结 4 1736
一向
一向 2020-12-08 04:30

Does the DOM have a hash-table of elements whose keys are the elements\' ids?
I want to know if document.getElementById looks up a hash table or traverses t

4条回答
  •  伪装坚强ぢ
    2020-12-08 05:15

    The specific implementation isn't defined in the HTML spec so it could easily vary browser to browser. For example IE documentation states

    Returns a reference to the first object with the specified value of the ID or NAME attribute.

    so I'd be tempted to say it does a search (or it just throws out elements in cases of hash collisions).

    EDIT Also keep in mind there are other data structures (like trees) that allow for access time somewhere between constant and linear.

提交回复
热议问题