Skip to content

Instantly share code, notes, and snippets.

@rcanepa
Created September 7, 2016 02:03
Show Gist options
  • Save rcanepa/2b4779cd6bb36d56f48b47d4a5302813 to your computer and use it in GitHub Desktop.
Save rcanepa/2b4779cd6bb36d56f48b47d4a5302813 to your computer and use it in GitHub Desktop.
Counting watchers on AngularJS

This Immediately Invoked Function Expression (IIFE) will print out the number of watchers currently on the page. Simply paste it into the console to see how many watchers are currently on the page. This IIFE was taken from Words Like Jared's answer on StackOverflow: http://stackoverflow.com/questions/18499909/how-to-count-total-number-of-watches-on-a-page

(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);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment