$watchCollection vs $watch

Benchmark created by c0bra on


Description

Cost of Angular's $watchCollection vs $watch

Preparation HTML

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

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

var deregs = [];

app.run(function($rootScope) {
  $rootScope.data = [
     'bob', 'frank', 'jerry', 'bargle',
     'bob', 'frank', 'jerry', 'bargle',
     'bob', 'frank', 'jerry', 'bargle',
     'bob', 'frank', 'jerry', 'bargle'
  ];
});

app.service('Watch', function($rootScope) {
  return {
    run: function() {
      deregs.push($rootScope.$watch('data', function(n, o) {
      
      }));
  
      $rootScope.$digest();
    }
  };
});

app.service('WatchCollection', function($rootScope) {
  return {
    run: function() {
      deregs.push($rootScope.$watchCollection('data', function(n, o) {

      }));
  
      $rootScope.$digest();
    }
  };
});
</script>

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

Setup

var i = angular.element(document.getElementById('myApp')).injector();

Teardown


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

Test runner

Ready to run.

Testing in
TestOps/sec
$watch
var s = i.get('Watch');
s.run();
ready
$watchCollection
var s = i.get('WatchCollection');
s.run();
ready

Revisions

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