Performance: switchMap for flattening
This affects how asynchronous streams are handled, impacting interaction responsiveness and CPU usage during observable flattening.
this.input$.pipe( switchMap(value => this.apiCall(value)) ).subscribe(result => this.handleResult(result));
this.input$.pipe( mergeMap(value => this.apiCall(value)) ).subscribe(result => this.handleResult(result));
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| mergeMap with rapid inputs | N/A | N/A | High CPU and network usage | [X] Bad |
| switchMap with rapid inputs | N/A | N/A | Minimal CPU and network usage | [OK] Good |
switchMap in Angular RxJS?switchMap in an Angular service?pipe() method on observables.observable$.pipe(switchMap(...)). observable$.switchMap(value => http.get(url)) is invalid because switchMap is not a method on observable directly. switchMap(observable$, value => http.get(url)) has wrong argument order. observable$.map(switchMap(value => http.get(url))) misuses map.const source$ = of('A', 'B', 'C');
source$.pipe(
switchMap(letter => of(letter + '1', letter + '2'))
).subscribe(console.log);switchMap:
this.searchTerm$.switchMap(term => this.http.get(`/api/search?q=${term}`))
.subscribe(results => this.results = results);searchTerm$.pipe( /* what operator here? */ )Pass the test and this pattern moves unknown