0
0
NestJSframework~8 mins

Cache decorators in NestJS - Performance & Optimization

Choose your learning style9 modes available
Performance: Cache decorators
MEDIUM IMPACT
Cache decorators improve response time by storing results and reducing repeated processing, impacting page load speed and interaction responsiveness.
Caching results of a controller method to speed up repeated requests
NestJS
@UseInterceptors(CacheInterceptor)
async getData() {
  return await this.service.fetchData();
}
Caches the result after first call, serving subsequent requests instantly without recomputation.
📈 Performance GainReduces server processing time and response delay, improving INP significantly
Caching results of a controller method to speed up repeated requests
NestJS
async getData() {
  // heavy computation or DB call
  return await this.service.fetchData();
}
Every request triggers the full computation or database call, causing slower responses and higher server load.
📉 Performance CostBlocks response for full computation on every request, increasing INP and server CPU usage
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
No cachingN/AN/AN/A[X] Bad
With cache decoratorsN/AN/AN/A[OK] Good
Rendering Pipeline
Cache decorators intercept method calls to serve cached data instead of recomputing, reducing backend processing before sending response to browser.
Server Processing
Network Transfer
⚠️ BottleneckServer Processing (heavy computation or DB calls)
Core Web Vital Affected
INP
Cache decorators improve response time by storing results and reducing repeated processing, impacting page load speed and interaction responsiveness.
Optimization Tips
1Cache results of expensive methods to reduce server processing time.
2Use cache decorators to serve repeated requests instantly.
3Monitor response times in Network tab to verify caching effectiveness.
Performance Quiz - 3 Questions
Test your performance knowledge
How do cache decorators improve web app performance?
ABy delaying server responses intentionally
BBy increasing the number of DOM nodes rendered
CBy storing method results to serve repeated requests faster
DBy adding extra CSS styles to speed up rendering
DevTools: Network
How to check: Open DevTools, go to Network tab, make repeated requests to the endpoint and observe response times.
What to look for: Faster response times on repeated requests indicate effective caching.