Performance: Creating observables
This affects how quickly data streams start emitting and how efficiently Angular handles asynchronous data updates.
const obs = new Observable(observer => { const intervalId = setInterval(() => { observer.next(Math.random()); }, 1000); return () => clearInterval(intervalId); });
const obs = new Observable(observer => { setInterval(() => { observer.next(Math.random()); }, 1000); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Observable with no cleanup | 0 | 0 | 0 | [X] Bad |
| Observable with cleanup on unsubscribe | 0 | 0 | 0 | [OK] Good |
new Observable() with a function receiving a subscriber.new Observable(subscriber => subscriber.next(...)).const obs = new Observable(subscriber => { subscriber.next(1); subscriber.next(2); subscriber.complete(); subscriber.next(3); });
obs.subscribe(value => console.log(value));const obs = new Observable(subscriber => { subscriber.next('Hi'); subscriber.error('Oops'); subscriber.next('Bye'); });Pass the test and this pattern moves unknown