19

I've heard from various ng-speakers how $watch is dangerous for performance of your application. I was wondering if anyone has compared performance of Rxjs' Observable against $watch in an AngularJS application. I know that Observables is going to be part of Angular 2.

DilumN
  • 2,889
  • 6
  • 30
  • 44
pchitre
  • 191
  • 1
  • 4

1 Answers1

25

The two mechanisms of observing changes are inherently different.

$watch is a brute force, pull-based mechanism. Where the observer is active and (generally) needs to visit each observed object / expression after any change happened. Surely the more to observe the slower the whole process.

Observable implements a push-based mechanism. Observer is passive and gets notified when something changed. Properly implemented it allows much more intelligent propagation of changes.


From what I know, using Observables in angular 2.0 is optional, but advised. Moreover, angular 2.0 is going to implement one-directional data flow similar to flux. Data changes propagate only downwards in DOM -- a component can directly observe / depend on data of their ancestors but not their descendants. After a change there is a guarantee that only some subtree of DOM needs update. In most cases this subtree will be much smaller than the whole DOM.

There is a great video from 2015 ng-conf benchmarking angular 1.x, react and angular 2.0. (not sure if it uses Observables though)


One last thing about Observable: it offers way more than the above description and it is a great way of dealing with asynchronous events.

artur grzesiak
  • 20,230
  • 5
  • 46
  • 56