$watchCollection vs $watch (v11)

Revision 11 of this benchmark created on


Description

Cost of Angular's $watchCollection vs $watch

Preparation HTML

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.js"></script>

<script>
var app = angular.module('app', []);

var deregs = [];

app.service('Watch', function($rootScope) {
  return {
    run: function() {
      deregs.push($rootScope.$watch('data', function(n, o) {
        if(n[0].obj.obj.boolean === true){
          console.log('yes im the truth');
        } else {
          console.log('boo im false');
        }
      },true));
  
      $rootScope.$digest();
    }
  };
});

app.service('WatchCollection', function($rootScope) {
  return {
    run: function() {
      deregs.push($rootScope.$watchCollection('data', function(n, o) {
        if(n[0].obj.obj.boolean === true){
          console.log('yes im the truth 2');
        } else {
          console.log('boo im false');
        }
      }));
  
      $rootScope.$digest();
    }
  };
});
</script>

<div id="myApp" ng-app="app"></div>

Setup

var i = angular.element(document.getElementById('myApp')).injector();
    var scope = i.get('$rootScope');
    scope.data = [
         {'bob': true, 'obj': {'obj': {'bool': false}}}, {'frank': false}, {'jerry': 'hey'}, {'bargle': false},
         {'bob': true}, {'bob': true}, {'frank': false}, {'jerry': 'hey'},           {'bargle': false},{'bob': true},{'bob': true}, {'frank': false}];

Teardown


    angular.forEach(deregs, function(d) {
      d();
    });
  

Test runner

Ready to run.

Testing in
TestOps/sec
$watch
var s = i.get('Watch');
s.run();
scope.data[0].obj.obj.boolean = true;
ready
$watchCollection
var s = i.get('WatchCollection');
s.run();
scope.data[0].obj.obj.boolean = true;
ready

Revisions

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