Performance: Reflector and custom decorators
MEDIUM IMPACT
This affects server-side request handling speed and memory usage by controlling metadata access and decorator execution.
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
import { ReflectMetadata } from '@nestjs/common'; @ReflectMetadata('roles', ['admin']) @Get('admin') getAdminData() { // handler code } // Access metadata manually in every request handler
| Pattern | Metadata Lookups | CPU Blocking | Request Latency | Verdict |
|---|---|---|---|---|
| Manual ReflectMetadata in each handler | Multiple per request | Minimal but repeated | Higher latency | [X] Bad |
| SetMetadata with Reflector in guards | Single per request | None | Lower latency | [OK] Good |
| Synchronous logic in decorators | N/A | Blocks event loop | Increased latency | [X] Bad |
| Asynchronous interceptors for logic | N/A | Non-blocking | Minimal latency | [OK] Good |