aeisenberg / angular-bind-notifier

On demand refreshing of AngularJS bindings. Vast performance improvements on complex apps.
http://aeisenberg.github.io/angular-bind-notifier/
MIT License
111 stars 13 forks source link

watching undefined variable causes many "fake" watchers to be created #36

Open yonihei opened 7 years ago

yonihei commented 7 years ago

In short: using the plugin on an undefined object causes the number of watchers to grow for no reason constantly.

ng-if=":auto:d.fakeVariable"

http://jsbin.com/titozudubo/edit?html,js,output (i hope it is working, but ill write here anyway the problem)

Explanation: i use a short code for finding the number watchers. i hope its is not buggy. Each time that the key "auto" has changed, it creates more watchers for no reason because d.fakeVariable is not defined.

here is the code for counting the number of watchers:

var getWatchers = function(root) { root = angular.element(root || document.documentElement); var watcherCount = 0;

  var getElemWatchers = function(element) {
    var isolateWatchers = getWatchersFromScope(element.data().$isolateScope);
    var scopeWatchers = getWatchersFromScope(element.data().$scope);
    var watchers = scopeWatchers.concat(isolateWatchers);
    angular.forEach(element.children(), function (childElement) {
      watchers = watchers.concat(getElemWatchers(angular.element(childElement)));
    });
    return watchers;
  }

  var getWatchersFromScope = function(scope) {
    if (scope) {
      return scope.$$watchers || [];
    } else {
      return [];
    }
  }

  return getElemWatchers(root);
};

console.log(getWatchers ().length);

fpoljak commented 7 years ago

You can override this until fixed by doing ng-if=":auto:(!!d.fakeVariable)"

ghost commented 6 years ago

Any updates on this? It seems to occur because the bindNotifier logic is piggybacking the one time binding workflow, and the default behaviour in one-time bindings is to keep a watcher until the value is undefined https://docs.angularjs.org/guide/expression#value-stabilization-algorithm

Heres a smaller example adapted from @yonihei 's jsbin https://jsbin.com/hovurekaqo/1/edit?html,js,output you can see the watcher count go up every 1s