Is there angular $ broadcast and $ on expensive?

I was told that using angular events can be expensive (I could not verify this)

Any calls $broadcastand $onshould be β€œwrapped” using a factory or service to enter their respective components to maintain performance?

Again, I would rather use $onand listen directly to the events, rather than creating a factory that essentially just registers the functions to be called when it receives the event - lets call it a dispatcher.

Please note that this is not only one component (directives) that listens to "some event", there will be many components that listen to this event.

Manager example:

angular.module('app').factory('dispatcher', ['$rootScope', function ($rootScope) {

    var registeredFns = [ ];

    $rootScope.$on('some-event', function (evt, msg) { 
        _.each(registeredFns, function (fn) {
            fn.apply(null, msg);
        });
    });

    return {
        onSomeEvent: function (fn) {
            registeredFns.push(fn);
        }
    };
});

, - , , , , .

+4
1

... , ( - !)

, , . $emit(), for() . , , "" , $rootScope.

, stopPropagation, .

$broadcast - , : (), child. , , $rootScope.$broadcast(), ngRepeats , , , , , Angular , .

, jsPerf , $rootScope.$emit() $rootScope.$on(). , .

+6

All Articles