0
0
NestJSframework~8 mins

Exception filters in NestJS - Performance & Optimization

Choose your learning style9 modes available
Performance: Exception filters
MEDIUM IMPACT
Exception filters affect the server response time and error handling flow, impacting how quickly errors are processed and sent to the client.
Handling errors globally in a NestJS application
NestJS
import { Catch, ExceptionFilter, ArgumentsHost, Logger } from '@nestjs/common';

@Catch()
export class GlobalFilter implements ExceptionFilter {
  private readonly logger = new Logger(GlobalFilter.name);

  catch(exception: any, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse();
    const status = exception.status || 500;
    // Asynchronous logging without blocking
    setImmediate(() => this.logger.error(exception.message));
    response.status(status).json({ message: exception.message });
  }
}
Moves heavy logging off the main thread to avoid blocking response sending.
📈 Performance GainResponse sent immediately, logging runs asynchronously, reducing latency by ~50-100ms
Handling errors 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
    for (let i = 0; i < 1000000; i++) { /* heavy loop */ }
    response.status(status).json({ message: exception.message });
  }
}
Heavy synchronous operations block the event loop delaying response and increasing latency.
📉 Performance CostBlocks response for 50-100ms depending on operation complexity
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Synchronous heavy processing in filter0 (server-side)0 (server-side)0 (server-side)[X] Bad
Asynchronous logging in filter0 (server-side)0 (server-side)0 (server-side)[OK] Good
Rendering Pipeline
Exception filters intercept errors during request processing and prepare the HTTP response. They affect the server's response time but do not directly impact browser rendering.
Server Processing
Response Preparation
⚠️ BottleneckSynchronous heavy operations inside filters block the event loop delaying response.
Optimization Tips
1Avoid heavy synchronous operations inside exception filters to prevent blocking the event loop.
2Use asynchronous or deferred logging to keep error handling fast.
3Exception filters impact server response time but do not directly affect browser rendering metrics.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance risk of doing heavy synchronous work inside a NestJS exception filter?
AIt causes layout shifts on the client side.
BIt blocks the event loop delaying the HTTP response.
CIt increases the browser's paint time.
DIt reduces the bundle size.
DevTools: Network and Performance panels
How to check: Open DevTools, go to Network tab, trigger an error response, check response time. Use Performance tab to record and analyze server response delays.
What to look for: Look for long server response times and blocking tasks delaying the response in the flame chart.