Performance: Injecting services into components
MEDIUM IMPACT
This affects the initial load time and runtime responsiveness by controlling how dependencies are created and shared in Angular components.
import { Component } from '@angular/core'; import { DataService } from './data.service'; @Component({ selector: 'app-example', templateUrl: './example.component.html' }) export class ExampleComponent { constructor(private dataService: DataService) {} } // DataService is providedIn: 'root' in its @Injectable decorator
import { Component } from '@angular/core'; import { DataService } from './data.service'; @Component({ selector: 'app-example', templateUrl: './example.component.html', providers: [DataService] }) export class ExampleComponent { constructor(private dataService: DataService) {} }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Service provided in component | No direct DOM impact | No reflows triggered | No paint cost | [X] Bad - increases CPU and memory usage |
| Service provided in root | No direct DOM impact | No reflows triggered | No paint cost | [OK] Good - efficient reuse of service instances |