$rootScope.emit() vs $rootScope.$broadcast() (v54)

Revision 54 of this benchmark created by newvs on


Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>

<script>
angular
    .module("testApp",[])
    .controller("testController",['$rootScope', '$scope','SharingService', function($rootScope, $scope, shSrv) {
		window.shSrv = shSrv;
        window.$rootScope = $rootScope;
        var items = $scope.items = [];

        for (i=0;i<100;i++){
            items.push({
                number: i,
                text: 'some text'
            })
        }
    }]).controller("liController", ['$rootScope','$scope','SharingService', function($rootScope, $scope, shSrv){
		$scope.$on("fooHappened", function() { $scope.item.number = $scope.item.number+"root";});
		shSrv.subscribe($scope, "CustomerChange", function(){$scope.item.number = $scope.item.number+"Srv";});
		
	}]).factory('SharingService', ['$timeout', '$filter', '$log', function($timeout, $filter, $log) {
		// Mapa de eventos subscritos
		var eventMap = {};
		var SharingService = {};	
		
		// Propaga los datos en la ngApp actual
		SharingService.publish = function(eventName, data){
			if(eventMap[eventName]) {
				var scopes = eventMap[eventName];
				for(var i=0,l=scopes.length; i<l; i++) {
					scopes[i].evFn.apply(scopes[i], [data]);
				}
			}
		};
		
		// Realiza la subscripcion a un determinado event
		SharingService.subscribe = function(scope, eventName, fn) {
			if(!eventMap[eventName]) {
				eventMap[eventName] = [];
			}
			var idSc = scope.$id || scope.id;
			eventMap[eventName].push({sc:idSc, evFn: fn});
			
			// Evento para eliminar el subscribe en caso de que se elimine el scope que hemos asociado
			scope.$on("$destroy", function() {
				SharingService.unsubscribe(scope);
			});
		};
		
		// Cancela la subcripcion de un evento. Si eventName es nulo/undefined, cancela la subsripcion de todos los eventos del scope.
		SharingService.unsubscribe = function(scope, eventName) {
			var idSc = scope.$id || scope.id;
			// Si no hemos pasado nombre de eventos, hacemos el unsubscribe de todos
			if(eventName==undefined) {
				for(var item in eventMap) {
					if(eventMap.hasOwnProperty(item)){
						eventName = item;
						deleteEvents();
					}
				}
			} else {
				if(eventMap[eventName]) {
					deleteEvents();
				}
			}
			
			function deleteEvents() {
				var scopes = angular.copy(eventMap[eventName]);
				for(var i=0,l=scopes.length; i<l; i++) {
					if(scopes[i].sc === idSc) {
						eventMap[eventName].splice(i,1);
					}
				}
			}
		};
		return SharingService;
	}]);;
</script>

<div ng-app="testApp">
    <div ng-controller="testController">
        <div ng-repeat="item in items">
				<div ng-controller="liController">{{item.number}}</div>
           </div>
    </div>
</div>

Test runner

Ready to run.

Testing in
TestOps/sec
$broadcast
window.$rootScope.$broadcast('fooHappened');
ready
$emit
window.$rootScope.$emit('fooHappened');
ready
SharingService
window.shSrv.publish("CustomerChange", {});
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.