Performance: Why design patterns matter
Design patterns affect how efficiently Angular apps load, render, and respond to user actions by organizing code for better performance and maintainability.
Jump into concepts and practice - no test required
Use Angular services with signals or RxJS observables to manage state and data flow cleanly.
Component directly manipulates DOM and shares state via global variables without Angular services or signals.
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Direct DOM manipulation in components | High (many nodes updated) | Multiple per action | High (many paints) | [X] Bad |
| State management with Angular services and signals | Low (targeted updates) | Single per action | Low (minimal paints) | [OK] Good |
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
@Injectable({ providedIn: 'root' })
export class DataService {
private dataSubject = new Subject<string>();
data$ = this.dataSubject.asObservable();
updateData(newData: string) {
this.dataSubject.next(newData);
}
}updateData('Hello') is called?import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
@Injectable({ providedIn: 'root' })
export class LoggerService {
private logSubject = new Subject<string>();
log$ = this.logSubject.asObservable();
logMessage(message: string) {
this.logSubject.next;
}
}this.logSubject.next; which does not call the function.this.logSubject.next(message); with parentheses and argument.