Skip to content

Multiple $watch can be a performance drag #6

@wjaspers

Description

@wjaspers

On the off-chance someone is using this more than once in the page, this adds more $watch functions than it needs. Only one is necessary, and only a single change detection can trigger the diff. Near simultaneous changes to left and right would otherwise create separate digest cycles.

Newer angular standards/preferences would suggest binding left and right to the controller, but for the sake of the current code, here's all that's needed.

var _this = this;
var unbindWatch = $scope.$watch(function () {
   return { left: $scope.left, right: $scope.right };
}, function (newValue, oldValue, localScope) {
  _this.doDiff();
}, true); 
$scope.$on("$destroy", unbindWatch);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions