Performance: Repository pattern
MEDIUM IMPACT
This pattern affects how data fetching and state management impact page load and interaction responsiveness.
class UserRepository { private userCache = null; constructor(private http: HttpClient) {} getUser() { if (this.userCache) return of(this.userCache); return this.http.get('/api/user').pipe(tap(data => this.userCache = data)); } } @Component({}) class ProfileComponent { constructor(private userRepo: UserRepository) {} ngOnInit() { this.userRepo.getUser().subscribe(); } } @Component({}) class DashboardComponent { constructor(private userRepo: UserRepository) {} ngOnInit() { this.userRepo.getUser().subscribe(); } }
class UserService { constructor(private http: HttpClient) {} getUser() { return this.http.get('/api/user'); } } @Component({}) class ProfileComponent { constructor(private userService: UserService) {} ngOnInit() { this.userService.getUser().subscribe(); } } @Component({}) class DashboardComponent { constructor(private userService: UserService) {} ngOnInit() { this.userService.getUser().subscribe(); } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Multiple fetches in components | Low | Low | High due to blocking | [X] Bad |
| Repository with caching | Low | Low | Low, non-blocking | [OK] Good |