$watchCollection vs $watch

JavaScript performance comparison

Revision 7 of this test case created by

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

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

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
$watchCollection
var s = i.get('WatchCollection');
s.run();
pending…
$watch
var s = i.get('Watch');
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.

0 Comments