This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
Optimize away broadcasts (and maybe emits) with no listeners #5341
Closed
Description
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:
- Have scope.$on register that an event key has a listener. Short circuit $broadcast if there are no listeners for a given key.
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. - Note if a registration is on $rootScope. For events that have only been registered on $rootScope, there's no need to walk down the tree.