You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
Hi, $rootScope.$broadcast/$scope.$on is almost perfect. It make us able to use $scope.$on for global events, this is perfect since we can handle local and global events the same way and it takes care of deleting listener (freeing memory) for us when $scope is destroyed.
Compared to it $rootScope.$emit/$rootScope.$on is faster but requires us to manually clear the callback to avoid leaking memory in listener.
$rootScope.$broadcast on the contrary is a little slow, cause it traverses all active scopes to find listeners to be triggered. Is it really necessary?
Why can't we solve the same problem in a different way?
function broadcast(evt) {
Object.keys(this.evtScopesMap[evt]).forEach(function(scope){
// Iterate scope.$$listeners and $emit;
})
}
Cleaning up
scope.destroy = function(){
// Iterate scope.$$listeners
// forEach event this scope is bound to:
delete $rootScope.evtScopesMap[evt][thisScope];
}
This should make $broadcast time constant in finding scopes at a small cost of a having an hashmap with scope references in $rootScope (cost in memory is linear to active scopes count).
I apologize if I misunderstood something here.
Thank you anyway
The text was updated successfully, but these errors were encountered:
We recently improved the performance of $rootScope.$broadcast considerable. See #5507
Have you looked at those?
It would be worth looking at the cost of these broadcasts with some real numbers before spending too much time trying to improve the performance. Do you have a concrete use case where $broadcast is too slow for you?
A good rule of thumb is that you should attach to the scope that follows
the same lifecycle. This is, a controller to $scope (using $on or
$onRootScope) and services to $rootScope, but avoid attaching in the later
case if possible.
@lgalfaso Thanks for your reply. What exactly do you mean with $onRootScope? As far as I understand this method does not really exist? (#5507 -> statement by Igor Minar: "In the light of all the performance improvements we've done since this issue was created, does it still make sense to add an api like $onRootScope?")
Hi,
$rootScope.$broadcast/$scope.$on
is almost perfect. It make us able to use$scope.$on
for global events, this is perfect since we can handle local and global events the same way and it takes care of deleting listener (freeing memory) for us when$scope
is destroyed.Compared to it
$rootScope.$emit/$rootScope.$on
is faster but requires us to manually clear the callback to avoid leaking memory in listener.$rootScope.$broadcast
on the contrary is a little slow, cause it traverses all active scopes to find listeners to be triggered. Is it really necessary?Why can't we solve the same problem in a different way?
Let's try to use a map in $rootScope:
Something like:
Listening:
Broadcasting:
Cleaning up
This should make $broadcast time constant in finding scopes at a small cost of a having an hashmap with scope references in $rootScope (cost in memory is linear to active scopes count).
I apologize if I misunderstood something here.
Thank you anyway
The text was updated successfully, but these errors were encountered: