0
0
NestJSframework~8 mins

Response transformation in NestJS - Performance & Optimization

Choose your learning style9 modes available
Performance: Response transformation
MEDIUM IMPACT
This affects how fast the server sends the final data to the client and how much processing happens before the response is sent.
Transforming API response data before sending to client
NestJS
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { map } from 'rxjs/operators';

@Injectable()
export class LightTransformInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler) {
    return next.handle().pipe(
      map(data => ({ ...data, value: data.value + 1 })),
    );
  }
}
Lightweight transformation avoids blocking event loop and sends response faster.
📈 Performance GainReduces response delay to under 10 ms, improving LCP.
Transforming API response data before sending to client
NestJS
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { map } from 'rxjs/operators';

@Injectable()
export class HeavyTransformInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler) {
    return next.handle().pipe(
      map(data => {
        // Heavy synchronous transformation
        for (let i = 0; i < 1000000; i++) {
          data.value += i;
        }
        return data;
      }),
    );
  }
}
Heavy synchronous processing blocks the event loop delaying response sending.
📉 Performance CostBlocks server response for 100+ ms depending on data size, increasing LCP.
Performance Comparison
PatternServer ProcessingResponse DelayImpact on LCPVerdict
Heavy synchronous transformationHigh CPU usage100+ ms delaySignificant LCP increase[X] Bad
Lightweight transformationMinimal CPU usageUnder 10 ms delayMinimal LCP impact[OK] Good
Rendering Pipeline
Response transformation happens on the server before the browser receives data. Heavy transformations delay the server sending the response, which delays the browser's rendering start.
Server Processing
Network Transfer
Browser Rendering Start
⚠️ BottleneckServer Processing (heavy synchronous transformations)
Core Web Vital Affected
LCP
This affects how fast the server sends the final data to the client and how much processing happens before the response is sent.
Optimization Tips
1Avoid heavy synchronous processing in response interceptors.
2Keep transformations simple and fast to reduce server response time.
3Use DevTools Network tab to monitor server response delays.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance risk of heavy synchronous response transformations in NestJS?
AThey block the event loop, delaying server response.
BThey increase client-side rendering time.
CThey reduce network bandwidth.
DThey cause layout shifts in the browser.
DevTools: Network
How to check: Open DevTools, go to Network tab, reload page, select API response, check 'Waiting (TTFB)' time.
What to look for: Long 'Waiting' time indicates server delay due to heavy response transformation.