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

Revision 79 of this benchmark created by Wilson Mendes on


Preparation HTML

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

<script>
angular
    .module('testApp',[])
    .controller('testController', testController)
    .controller('anotherTestController', anotherTestController);
    
    function testController($rootScope, $scope) {

        window.$rootScope = $rootScope;
        var items = $scope.items = [];

        for (i=0;i<100;i++){
            items.push({
                number: i,
                text: 'some text'
            })
        }
        
        $scope.$on('fooHappened', function(){
            for (i=0;i<100;i++){
                items.push({
                    number: i,
                    text: 'Add some text via $on event'
                })
            }
        });
    }
    testController.$inject = ['$scope'];


    function anotherTestController($rootScope, $scope) {
        var items = $scope.items = [];

        for (i=0;i<100;i++){
            items.push({
                number: i,
                text: 'some text'
            })
        }
        
        $scope.$on('fooHappened', function(){
            for (i=0;i<100;i++){
                items.push({
                    number: i,
                    text: 'Add some text via $on event'
                })
            }
        });
    }
    anotherTestController.$inject = ['$scope'];
</script>

<div ng-app="testApp">
    <div ng-controller="testController">
        <ul>
            <li ng-repeat="item in items" ng-bind="item.number">
            </li>
        </ul>
    </div>
</div>

Test runner

Ready to run.

Testing in
TestOps/sec
$emit
window.$rootScope.$emit('fooHappened');
ready
$broadcast
window.$rootScope.$broadcast('fooHappened');
ready

Revisions

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