$watchCollection vs $watch

JavaScript performance comparison

Revision 7 of this test case created

Info

Cost of Angular's $watchCollection vs $watch, (collection size 10)

Preparation code

<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 = [];

  for(var i = 0; i< 10 ; i++){
   $rootScope.data.push('item:'+i);
  }
});

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>
<script>
Benchmark.prototype.setup = function() {
    var i = angular.element(document.getElementById('myApp')).injector();
};

Benchmark.prototype.teardown = function() {
    angular.forEach(deregs, function(d) {
      d();
    });
};
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
$watch
var s = i.get('Watch');
s.run();
pending…
$watchCollection
var s = i.get('WatchCollection');
s.run();
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment