0
0
NestJSframework~8 mins

Global exception filters in NestJS - Performance & Optimization

Choose your learning style9 modes available
Performance: Global exception filters
MEDIUM IMPACT
Global exception filters affect the response time and error handling flow, impacting interaction responsiveness and server response speed.
Handling all exceptions globally in a NestJS application
NestJS
import { Catch, ExceptionFilter, ArgumentsHost } from '@nestjs/common';

@Catch()
export class GlobalFilter implements ExceptionFilter {
  async catch(exception: any, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse();
    const status = exception.status || 500;
    // Use async non-blocking logging
    await asyncLogging(exception);
    response.status(status).json({ message: 'Error occurred' });
  }
}
Using asynchronous non-blocking operations avoids delaying the response, improving interaction responsiveness.
📈 Performance GainReduces blocking time to near 0ms, improving INP significantly.
Handling all exceptions globally in a NestJS application
NestJS
import { Catch, ExceptionFilter, ArgumentsHost } from '@nestjs/common';

@Catch()
export class GlobalFilter implements ExceptionFilter {
  catch(exception: any, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse();
    const status = exception.status || 500;
    // Synchronous heavy logging or blocking operation here
    heavySyncLogging(exception);
    response.status(status).json({ message: 'Error occurred' });
  }
}
Blocking synchronous operations inside the filter delay response sending, increasing interaction latency.
📉 Performance CostBlocks response for 50-200ms depending on logging complexity, increasing INP.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Synchronous blocking logging in global filter0 (server-side)0 (server-side)0 (server-side)[X] Bad
Asynchronous non-blocking logging in global filter0 (server-side)0 (server-side)0 (server-side)[OK] Good
Rendering Pipeline
Global exception filters intercept errors during request handling before the response is sent. They affect the server response phase, which impacts how quickly the browser receives the final content.
Request Handling
Response Generation
⚠️ BottleneckBlocking synchronous operations inside the filter delay response generation.
Core Web Vital Affected
INP
Global exception filters affect the response time and error handling flow, impacting interaction responsiveness and server response speed.
Optimization Tips
1Avoid synchronous blocking code inside global exception filters.
2Use asynchronous logging or error reporting to keep response fast.
3Test error response times in Network tab to detect slow filters.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance risk of using synchronous operations inside a global exception filter in NestJS?
AIt blocks the response, increasing interaction latency.
BIt increases DOM nodes on the client.
CIt causes layout shifts in the browser.
DIt reduces bundle size.
DevTools: Network
How to check: Open DevTools, go to Network tab, trigger an error response, and check the response time for error requests.
What to look for: Look for long response times on error requests indicating blocking operations in exception filters.