Performance: Custom pipes
MEDIUM IMPACT
Custom pipes affect request processing speed and server response time by transforming or validating data before controller logic.
import { PipeTransform, Injectable, ArgumentMetadata } from '@nestjs/common'; @Injectable() export class LightweightPipe implements PipeTransform { transform(value: any, metadata: ArgumentMetadata) { // Simple validation or transformation if (typeof value !== 'string') { throw new Error('Invalid type'); } return value.trim(); } }
import { PipeTransform, Injectable, ArgumentMetadata } from '@nestjs/common'; @Injectable() export class HeavyPipe implements PipeTransform { transform(value: any, metadata: ArgumentMetadata) { // Heavy synchronous computation for (let i = 0; i < 1e8; i++) {} return value; } }
| Pattern | CPU Usage | Event Loop Blocking | Latency Impact | Verdict |
|---|---|---|---|---|
| Heavy synchronous computation | High | Blocks event loop | High latency | [X] Bad |
| Lightweight synchronous validation | Low | No blocking | Low latency | [OK] Good |
| Slow async external calls | Medium | Blocks async flow | High latency | [X] Bad |
| Fast local async checks | Low | No blocking | Low latency | [OK] Good |