0
0
NestJSframework~8 mins

Controller decorator in NestJS - Performance & Optimization

Choose your learning style9 modes available
Performance: Controller decorator
MEDIUM IMPACT
This affects the routing setup and initial request handling speed in a NestJS application.
Defining routes for handling HTTP requests in a NestJS app
NestJS
import { Controller, Get } from '@nestjs/common';

@Controller('api')
export class AppController {
  @Get('users')
  getUsers() {
    // logic
  }

  @Get('products')
  getProducts() {
    // logic
  }

  @Get('orders')
  getOrders() {
    // logic
  }
}
Prefixing routes with a common path segment reduces routing complexity and speeds up route matching.
📈 Performance GainReduces routing checks per request, improving INP by lowering request handling latency.
Defining routes for handling HTTP requests in a NestJS app
NestJS
import { Controller, Get } from '@nestjs/common';

@Controller('')
export class AppController {
  @Get('users')
  getUsers() {
    // logic
  }

  @Get('products')
  getProducts() {
    // logic
  }

  @Get('orders')
  getOrders() {
    // logic
  }
}
Using an empty string in @Controller decorator causes all routes to be registered at the root, increasing routing checks and complexity.
📉 Performance CostAdds unnecessary routing overhead, increasing request matching time slightly for each route.
Performance Comparison
PatternRouting ChecksRequest LatencyCode ComplexityVerdict
Empty string prefix in @ControllerHigh (all routes at root)Higher latency due to more checksModerate[X] Bad
Specific prefix in @Controller (e.g., 'api')Lower (grouped routes)Lower latency with faster matchingModerate[OK] Good
Rendering Pipeline
The Controller decorator defines route prefixes that the NestJS router uses to match incoming HTTP requests. Efficient route prefixing reduces the number of checks the router performs before invoking the handler.
Routing
Request Handling
⚠️ BottleneckRouting stage due to inefficient route matching
Core Web Vital Affected
INP
This affects the routing setup and initial request handling speed in a NestJS application.
Optimization Tips
1Always use specific route prefixes in @Controller to group related routes.
2Avoid empty or overly broad prefixes that cause routing overhead.
3Keep route definitions clear to minimize routing checks and improve responsiveness.
Performance Quiz - 3 Questions
Test your performance knowledge
How does using a specific prefix in the @Controller decorator affect routing performance?
AIt reduces routing checks by grouping routes under a common path.
BIt increases routing checks by adding more routes.
CIt has no effect on routing performance.
DIt blocks rendering until all routes are loaded.
DevTools: Network and Performance panels
How to check: Use the Network panel to measure response times for API calls. Use the Performance panel to record and analyze request handling duration.
What to look for: Look for lower request latency and faster handler invocation times indicating efficient routing.