Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utilize event deduping optmization with addEventListener #1965

Merged
merged 1 commit into from
Dec 9, 2015

Conversation

samccone
Copy link
Contributor

@samccone samccone commented Dec 9, 2015

TLDR; ✨ ❤️

The old fix for the leak, did accomplish removing the event listener leak, however it was fixed in a way that caused object thrashing due to the non-uniqueness of each event-listener bound fn (due to the use of native bind)


The initial leak found here #761

And then patched here 957656d

Was never the "correct" fix. Yes the patch in
957656d caused us to no longer leak
however we were still paying the cost of registering and unregistering
for the same event over and over, as well as allocating a new bound
scope each time (thus generating more garbage and thus eventually
causing more time to be spent in GC)

screen shot 2015-12-08 at 7 10 24 pm


Instead I opted here to take advantage of addEventListener and its
ability to automatically discard duplicate listeners. (1)

Resulting in a much more efficient use of runtime memory heap allocations (and thus less time to be spent in GC land)

screen shot 2015-12-08 at 7 12 02 pm

💛

Because there is no outside pointers into this eventListener (such as .bind or a parent self reference), there is no need to remove the listener anymore, because once the node reference becomes
unreachable the events will be automatically GCd for us. 🎲


(1)

If multiple identical EventListeners are registered on the same
EventTarget with the same parameters, the duplicate instances are
discarded. They do not cause the EventListener to be called twice, and
they do not need to be removed manually with the removeEventListener
method.
https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Multiple_identical_event_listeners

The initial leak found here
#761

And then patched here
957656d

Was never the "correct" fix. Yes the patch in
957656d cause us to no longer leak
however we were still paying the cost of registering and unregistering
for the same event over and over, as well as allocating a new bound
scope each time (thus generating more garbage and thus eventually
causing more time to be spent int GC)

Instead I opted here to take advantage of addEventListener and its
ability to automatically discard duplicate listeners.

> If multiple identical EventListeners are registered on the same
> EventTarget with the same parameters, the duplicate instances are
> discarded. They do not cause the EventListener to be called twice, and
> they do not need to be removed manually with the removeEventListener
> method.
https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Multiple_identical_event_listeners

Because there is no outside pointers into this event, there is no need
to remove the listener anymore, because once the node reference becomes
unreachable the events will be automatically GCd for us.

:)
@addyosmani
Copy link
Contributor

Great work, Sam. This initially LGTM

@surma
Copy link
Contributor

surma commented Dec 9, 2015

LGTM :) Awesome stuff 🎉

@sgomes Imma leave it to you to decide whether to merge

@sgomes
Copy link
Contributor

sgomes commented Dec 9, 2015

Looks 1.1-worthy to me. Thanks @samccone! :)

sgomes added a commit that referenced this pull request Dec 9, 2015
Utilize event deduping optmization with addEventListener
@sgomes sgomes merged commit 96e7ea0 into master Dec 9, 2015
@sgomes sgomes deleted the sjs/menu-leak-take2 branch December 9, 2015 15:52
@samccone
Copy link
Contributor Author

samccone commented Dec 9, 2015

🙇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants