0
0
Angularframework~8 mins

Why services are needed in Angular - Performance Evidence

Choose your learning style9 modes available
Performance: Why services are needed
MEDIUM IMPACT
This concept affects how efficiently data and logic are shared across components, impacting rendering speed and user interaction responsiveness.
Sharing data and logic between multiple components
Angular
Create a singleton Angular service that fetches data once and shares it via Observables to ComponentA and ComponentB.
Data is fetched once, cached, and shared, reducing network load and avoiding repeated rendering.
📈 Performance GainSingle network request and fewer change detection runs, improving INP.
Sharing data and logic between multiple components
Angular
ComponentA and ComponentB each fetch the same data independently inside ngOnInit() without a shared service.
This causes duplicate HTTP requests and redundant processing, increasing load time and CPU usage.
📉 Performance CostTriggers multiple network requests and duplicate change detection cycles.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Duplicate data fetching in componentsMultiple redundant DOM updatesMultiple reflows due to repeated change detectionHigher paint cost from frequent updates[X] Bad
Shared data via singleton serviceMinimal DOM updates triggered by shared ObservablesSingle reflow per data changeLower paint cost due to fewer updates[OK] Good
Rendering Pipeline
Services centralize data and logic, reducing redundant DOM updates and network calls. Components subscribe to service data streams, triggering fewer change detection cycles.
Network
JavaScript Execution
Change Detection
Rendering
⚠️ BottleneckExcessive network requests and repeated change detection caused by duplicated logic in components.
Core Web Vital Affected
INP
This concept affects how efficiently data and logic are shared across components, impacting rendering speed and user interaction responsiveness.
Optimization Tips
1Use singleton services to share data and logic across components.
2Avoid fetching the same data multiple times in different components.
3Centralize state management in services to reduce redundant rendering.
Performance Quiz - 3 Questions
Test your performance knowledge
What is a main performance benefit of using Angular services to share data?
AIncreases bundle size significantly
BReduces duplicate network requests and redundant rendering
CTriggers more change detection cycles
DBlocks rendering until all components load
DevTools: Performance
How to check: Record a session while interacting with components that fetch data. Look for multiple network requests and repeated scripting or rendering tasks.
What to look for: Multiple identical network requests and frequent scripting tasks indicate redundant data fetching; fewer requests and smoother scripting indicate good service use.