Why does Angular's Change Detector not use Proxy objects

484 Views Asked by At

Angular (precisely: zone.js) monkey-patches functions like setTimeout(), event listeners and similar, in order to fire the Change Detector when the respective callback got executed. However, Angular does not know which objects got updated and which remained unchanged. So, for every property used in the template, Angular has to check whether the property has been changed or not. This does not seem to be a very performant approach.

My question: Why does Angular not use Proxy objects for this? Proxy objects would allow Angular to exactly determine what has been changed, without having to compare the entire state tree. So is there any particular reason why the Angular devs chose not to use Proxy objects (as used in Vue)?

Btw: One advantage of a Proxy object could be that one could call functions in templates without causing additional/unnecessary CD cycles:

<div *ngFor="let item of myFunction()">...</div>
1

There are 1 best solutions below

1
On

Because there is a much nicer way to not have to check everything with every change: Observables!

If you use observables together with the async pipe consistently everywhere, Angular will know exactly what changed and when. No need for heavyweight proxies or other things that would emulate this behavior etc.

If you set up your services and pages to only provide observables, it will be as clean as it gets since you can use them directly in HTML.

Furthermore you should also deactivate polling when only using observables. However, I could not find the setting just now (been a while).