0
0
NestJSframework~8 mins

Route handlers (GET, POST, PUT, DELETE) in NestJS - Performance & Optimization

Choose your learning style9 modes available
Performance: Route handlers (GET, POST, PUT, DELETE)
MEDIUM IMPACT
This affects server response time and client perceived latency by how efficiently route handlers process requests and send responses.
Handling HTTP requests with route handlers in NestJS
NestJS
import { Controller, Get } from '@nestjs/common';

@Controller('items')
export class ItemsController {
  @Get()
  async getAll() {
    // Asynchronous non-blocking operation
    const data = await this.fetchItemsFromDb();
    return { data };
  }

  async fetchItemsFromDb() {
    // Simulate async DB call
    return new Promise(resolve => setTimeout(() => resolve(['item1', 'item2']), 50));
  }
}
Using async/await avoids blocking the event loop, allowing other requests to be processed concurrently.
📈 Performance GainNon-blocking, reduces server response delay, improves INP metric.
Handling HTTP requests with route handlers in NestJS
NestJS
import { Controller, Get } from '@nestjs/common';

@Controller('items')
export class ItemsController {
  @Get()
  getAll() {
    // Synchronous heavy computation or blocking code
    for (let i = 0; i < 1e9; i++) {}
    return { data: 'All items' };
  }
}
Blocking synchronous code delays response, causing slow server replies and poor user experience.
📉 Performance CostBlocks event loop, increasing response time by hundreds of milliseconds or more.
Performance Comparison
PatternServer ProcessingEvent Loop BlockingResponse TimeVerdict
Synchronous blocking handlersHigh CPU usageBlocks event loopSlow responses[X] Bad
Asynchronous non-blocking handlersEfficient CPU usageNo blockingFast responses[OK] Good
Rendering Pipeline
Route handlers process incoming HTTP requests on the server, affecting how quickly responses are generated and sent back to the client. Slow or blocking handlers delay response generation, increasing interaction latency.
Server Processing
Network Response
Client Rendering
⚠️ BottleneckServer Processing (event loop blocking or heavy synchronous work)
Core Web Vital Affected
INP
This affects server response time and client perceived latency by how efficiently route handlers process requests and send responses.
Optimization Tips
1Avoid synchronous blocking code in route handlers to keep the event loop free.
2Use async/await and non-blocking calls for database or heavy operations.
3Monitor server response times to detect slow route handlers affecting user experience.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance problem with synchronous blocking code in NestJS route handlers?
AIt causes layout shifts in the browser
BIt increases bundle size significantly
CIt blocks the event loop, delaying all other requests
DIt improves server throughput
DevTools: Network and Performance panels
How to check: Use Network panel to measure response times of API calls. Use Performance panel to record server response delays and event loop blocking.
What to look for: Look for long server response times and event loop blocking indicators that show slow route handler execution.