0
0
NestJSframework~8 mins

Reflector and custom decorators in NestJS - Performance & Optimization

Choose your learning style9 modes available
Performance: Reflector and custom decorators
MEDIUM IMPACT
This affects server-side request handling speed and memory usage by controlling metadata access and decorator execution.
Accessing metadata in request handlers using decorators
NestJS
import { SetMetadata, Reflector } from '@nestjs/core';

const Roles = (...roles: string[]) => SetMetadata('roles', roles);

@Roles('admin')
@Get('admin')
getAdminData() {
  // handler code
}

// Use Reflector in guards or interceptors to access metadata once per request
Centralizes metadata access with Reflector, reducing repeated lookups and improving request processing efficiency.
📈 Performance GainSingle metadata retrieval per request, lowering CPU usage and improving throughput.
Accessing metadata in request handlers using decorators
NestJS
import { ReflectMetadata } from '@nestjs/common';

@ReflectMetadata('roles', ['admin'])
@Get('admin')
getAdminData() {
  // handler code
}

// Access metadata manually in every request handler
Using ReflectMetadata repeatedly causes redundant metadata storage and manual retrieval, increasing processing time per request.
📉 Performance CostAdds extra metadata lookups per request, increasing CPU usage and slowing response time.
Performance Comparison
PatternMetadata LookupsCPU BlockingRequest LatencyVerdict
Manual ReflectMetadata in each handlerMultiple per requestMinimal but repeatedHigher latency[X] Bad
SetMetadata with Reflector in guardsSingle per requestNoneLower latency[OK] Good
Synchronous logic in decoratorsN/ABlocks event loopIncreased latency[X] Bad
Asynchronous interceptors for logicN/ANon-blockingMinimal latency[OK] Good
Rendering Pipeline
In NestJS, Reflector and custom decorators affect the server-side request lifecycle by adding metadata and logic before controller methods execute. Metadata retrieval happens during guard or interceptor phases, influencing CPU usage and response time.
Metadata Reflection
Request Handling
Middleware/Interceptor Execution
⚠️ BottleneckExcessive or synchronous logic in decorators can block the event loop, increasing request latency.
Optimization Tips
1Use Reflector with SetMetadata to minimize repeated metadata lookups.
2Avoid synchronous blocking logic inside custom decorators.
3Move heavy or side-effect logic to asynchronous interceptors or middleware.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance benefit of using Reflector with SetMetadata in NestJS?
AIt reduces repeated metadata lookups per request.
BIt automatically caches all database queries.
CIt blocks the event loop to ensure synchronous execution.
DIt increases bundle size significantly.
DevTools: Performance (Node.js profiling)
How to check: Run the NestJS app with Node.js --inspect flag, open Chrome DevTools, record CPU profile during requests, and analyze blocking operations.
What to look for: Look for synchronous blocking in decorator functions and repeated metadata reflection calls increasing CPU time.