angularjs - How to count total number of watches on a page? -


is there way, in javascript, count number of angular watches on entire page?

we use batarang, doesn't suit our needs. our application big , we're interested in using automated tests check if watch count goes much.

it useful count watches on per-controller basis.

edit: here attempt. counts watches in class ng-scope.

(function () {     var elts = document.getelementsbyclassname('ng-scope');     var watches = [];     var visited_ids = {};     (var i=0; < elts.length; i++) {        var scope = angular.element(elts[i]).scope();        if (scope.$id in visited_ids)           continue;        visited_ids[scope.$id] = true;        watches.push.apply(watches, scope.$$watchers);     }     return watches.length; })(); 

(you may need change body html or wherever put ng-app)

(function () {      var root = angular.element(document.getelementsbytagname('body'));      var watchers = [];      var f = function (element) {         angular.foreach(['$scope', '$isolatescope'], function (scopeproperty) {              if (element.data() && element.data().hasownproperty(scopeproperty)) {                 angular.foreach(element.data()[scopeproperty].$$watchers, function (watcher) {                     watchers.push(watcher);                 });             }         });          angular.foreach(element.children(), function (childelement) {             f(angular.element(childelement));         });     };      f(root);      // remove duplicate watchers     var watcherswithoutduplicates = [];     angular.foreach(watchers, function(item) {         if(watcherswithoutduplicates.indexof(item) < 0) {              watcherswithoutduplicates.push(item);         }     });      console.log(watcherswithoutduplicates.length); })(); 
  • thanks erilem pointing out answer missing $isolatescope searching , watchers potentially being duplicated in his/her answer/comment.

  • thanks ben2307 pointing out 'body' may need changed.


original

i did same thing except checked data attribute of html element rather class. ran yours here:

http://fluid.ie/

and got 83. ran mine , got 121.

(function () {      var root = $(document.getelementsbytagname('body'));     var watchers = [];      var f = function (element) {         if (element.data().hasownproperty('$scope')) {             angular.foreach(element.data().$scope.$$watchers, function (watcher) {                 watchers.push(watcher);             });         }          angular.foreach(element.children(), function (childelement) {             f($(childelement));         });     };      f(root);      console.log(watchers.length); })(); 

i put in mine:

for (var = 0; < watchers.length; i++) {     (var j = 0; j < watchers.length; j++) {         if (i !== j && watchers[i] === watchers[j]) {             console.log('here');         }     } } 

and nothing printed out, i'm guessing mine better (in found more watches) - lack intimate angular knowledge know sure mine isn't proper subset of solution set.


Comments

Popular posts from this blog

design - Custom Styling Qt Quick Controls -

Unable to remove the www from url on https using .htaccess -