What's the difference between live and not live collection in Javascript selectors?

前端 未结 2 2024
情书的邮戳
情书的邮戳 2021-02-08 09:40

How can I know what is the difference between live and not live collection.

According to my research:

A live is: when the changes in

2条回答
  •  名媛妹妹
    2021-02-08 09:45

    These

    document.getElementsByClassName()
    document.getElementsByTagName()
    document.getElementsByName()
    

    are live because they are observers of internal collections maintained by engines. That maintenance is not strictly required but is easy to achieve.

    document.querySelectorAll() 
    

    is not live because result gets computed each time you request it. Maintenance of live collection is too expensive as each modification (content, attributes, classes) of the DOM in this case will require re-evaluation of each element in the collection - O(N*M) task where N is the number of all elements in the DOM (worst case) and M number of active querySelectorAll() collections.

提交回复
热议问题