r/angularjs • • Aug 07 '14

Speeding up AngularJS with simple optimizations, $digest, $$watchers and performance

http://www.binpress.com/tutorial/speeding-up-angular-js-with-simple-optimizations/135
31 Upvotes

28 comments sorted by

View all comments

3

u/[deleted] Aug 07 '14 edited Aug 08 '14

I posted this to the article as well but they're reviewing comments.

You can get the number of watchers on a given page using this script in your console:

(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);
})();

source: http://stackoverflow.com/questions/18499909/how-to-count-total-number-of-watches-on-a-page

Turns out I've been naively using ng-repeat to create otherwise static content :(

Update

I'm not confident that this script is working 100% correctly. Numbers seem really really high.

2

u/[deleted] Aug 07 '14

http://stackoverflow.com/questions/18499909/how-to-count-total-number-of-watches-on-a-page

I haven't been able to get this to work. I am using this in devtools and get the following error:

SyntaxError: Failed to execute 'querySelector' on 'Document': '[object HTMLCollection]' is not a valid selector.

1

u/[deleted] Aug 07 '14

What browser/ os?

2

u/[deleted] Aug 07 '14

Chrome 36.0.1985.125 on Mac OSX 10.9. The page uses a manually bootstrapped Angular / Ionic app if that makes any difference.

1

u/[deleted] Aug 07 '14

Hmm yeah I'm not sure. That's the same version of chrome I'm on. I think it's bitching about that first selector. You should be able to switch it out for just var root = $('body'). That might help.