Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

perf(Scope): watcher deduplication #5829

Closed
kseamon opened this issue Jan 15, 2014 · 4 comments
Closed

perf(Scope): watcher deduplication #5829

kseamon opened this issue Jan 15, 2014 · 4 comments

Comments

@kseamon
Copy link
Contributor

kseamon commented Jan 15, 2014

scope.$watch('foo', function() {do.this()});
scope.$watch('foo', function() {do.that()});
scope.$watch('foo', function() {do.theOther()});

At present, this would add three dirty checks to the digest cycle. Since they are all watching the same scope expression, we could instead add one dirty check that fires all three change functions as appropriate.

The impact of this change should probably be tested before committing it. It might not be worthwhile in practice.

@Narretz
Copy link
Contributor

Narretz commented Jan 16, 2014

Could be worthwhile for large deepwatches or $watchCollections, as in ng-repeated elements.

@kseamon
Copy link
Contributor Author

kseamon commented Jan 17, 2014

It won't actually be much help for ng-repeat, as each iteration gets its own scope. This optimization would only help when there are multiple identical watchers on the same scope. It's not that this never happens, but this is why I think the impact needs some examination.

@greglockwood
Copy link

In the newly released ng-computed module for Angular, there is a "$batchedWatch" service that can be used as a drop-in replacement for regular $scope.$watch. It will correctly deduplicate the watches as mentioned. It even caters for the case where you have different types of watches (reference/shallow and equality/deep).

It may be split out into its own module and/or project at some point in the future.

Disclaimer: the module was written by one of my coworkers.

@Narretz
Copy link
Contributor

Narretz commented Jan 22, 2016

Same idea with a bit more discussion: #13731

@Narretz Narretz closed this as completed Jan 22, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants