-
Notifications
You must be signed in to change notification settings - Fork 27.5k
Optimize away broadcasts (and maybe emits) with no listeners #5341
Comments
instead of having a global registry, create a counter on each scope that will tell us that the current scope or some of its children have a listener for event X. This means that on each registration or deregistration and scope destcruction we would have to traverse the scope tree from the current scope up to the root scope and decrement the counters. But given that the registration of event listeners is a relatively rare event it should be super cheap. |
I totally agree with your conclusion here. Furthermore I see it as anti pattern when library authors write This is why I came up with the proposal of Given the amount of upvotes this technique received on StackOverflow you could actually call it a best practice I guess: I would love to see it addressed in core but so far the technique received a push back from the core team (See #4574) |
PR is in the works. Stay tuned. |
…stered the event Update $on and $destroy to maintain a count of event keys registered for each scope and its children. $broadcast will not descend past a node that has a count of 0/undefined for the $broadcasted event key. Resolves angular#5341
…stered the event Update $on and $destroy to maintain a count of event keys registered for each scope and its children. $broadcast will not descend past a node that has a count of 0/undefined for the $broadcasted event key. Resolves angular#5341
…stered the event Update $on and $destroy to maintain a count of event keys registered for each scope and its children. $broadcast will not descend past a node that has a count of 0/undefined for the $broadcasted event key. Resolves angular#5341
…stered the event Update $on and $destroy to maintain a count of event keys registered for each scope and its children. $broadcast will not descend past a node that has a count of 0/undefined for the $broadcasted event key. Resolves angular#5341
…eners for the event Update $on and $destroy to maintain a count of event keys registered for each scope and its children. $broadcast will not descend past a node that has a count of 0/undefined for the $broadcasted event key. Closes angular#5341
…eners for the event Update $on and $destroy to maintain a count of event keys registered for each scope and its children. $broadcast will not descend past a node that has a count of 0/undefined for the $broadcasted event key. Closes angular#5341 Closes angular#5371
…eners for the event Update $on and $destroy to maintain a count of event keys registered for each scope and its children. $broadcast will not descend past a node that has a count of 0/undefined for the $broadcasted event key. Closes angular#5341 Closes angular#5371
On my app's more complex pages with lots of nested scopes, $broadcast can become expensive, eating up 30ms for a $location change and 10-20 more if it results in a route change.
Most if not all of the events broadcasted in the process are not used by my app.
I propose the following two optimizations:
1a) Extra credit: store a "reference count" along with the registration and decrement it on scope.$destroy in order to keep the registry garbage collected.
The text was updated successfully, but these errors were encountered: