webcomponents / polyfills

Web Components Polyfills
BSD 3-Clause "New" or "Revised" License
1.14k stars 165 forks source link

[ShadyDOM] Adding event listeners leaks memory and is slow when the same function is used for many listeners #175

Open sorvell opened 5 years ago

sorvell commented 5 years ago

Related issue: https://github.com/Polymer/polymer/issues/5545.

When ShadyDOM creates an event listener, it stores the node on which the event is listening on the listener function. If the same function is used for many listeners, for example when a library function is used as an event listener, then the number of nodes stored on the listening function can become large. If listeners are not explicitly disposed of with removeEventListener, the node cannot be disposed of when it otherwise would, for example, when it's removed from the page.

To address this, we can store the listener function on the node being listened to instead. This should be fine since add/remove are called on the node. The info could be stored as a Map with the listener function as key storying an object with listened to event names and an array of event options.

sorvell commented 5 years ago

Note, this is an issue in both the v1 and v2 version of the polyfills.

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.