jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
This test compares the impact of event bubbling in an angular application and compares it against a "flat" event bus provided by $rootScope.$emit.
The good thing is there should barely cases where you actually need $broadcast. Learn how to avoid it here:
http://stackoverflow.com/a/19498009/288703
the only reason $broadcast in that test outperforms $emit - is because there are 0 listeners to the event in it's $$listeners dict. - basically it does nothing in that test - that's why it's so much faster. Same exact results will be for any angular version for that reason - I've tried 1.4.5 as the highest. But try to add any variation of listener there and $emit and $broadcast will be almost even, but $emit is slightly faster anyways.
So I've updated the new revision with listeners to see the actual picture
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0/angular.min.js"></script>
<script>
angular
.module("testApp",[])
.controller("testController",function($rootScope, $scope) {
window.$rootScope = $rootScope;
var items = $scope.items = [];
for (i=0;i<100;i++){
items.push({
number: i,
text: 'some text'
})
}
}).directive('superItem', function ($rootScope) {
return {
restrict: 'A',
link: function (scope, element, attr) {
window.$rootScope = window.$rootScope || $rootScope;
window.$rootScope.$on('fooHappened', function(e) {
var ok = 1;
});
}
};
});
</script>
<div ng-app="testApp">
<div ng-controller="testController">
<ul ng-repeat="item in items">
<li super-item ng-bind="item.number"></li>
</ul>
</div>
</div>
Ready to run.
Test | Ops/sec | |
---|---|---|
$broadcast with listeneres |
| ready |
$emit with listeneres |
| ready |
$broadcast with NO listeners |
| ready |
$emit with NO listeners |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.