Performance: Why DTOs enforce data contracts
MEDIUM IMPACT
This concept affects the speed and reliability of data validation and transformation during API request handling, impacting server response time and error handling.
import { IsString, IsEmail } from 'class-validator'; export class CreateUserDto { @IsString() name: string; @IsEmail() email: string; } async createUser(data: CreateUserDto) { // Data validated and structured by DTO const user = await this.userService.create(data); return user; }
async createUser(data: any) { // No validation or structure enforcement const user = await this.userService.create(data); return user; }
| Pattern | Validation Overhead | Error Handling Cost | Response Time Impact | Verdict |
|---|---|---|---|---|
| No DTOs, untyped data | None upfront | High due to runtime errors | Slower due to retries | [X] Bad |
| DTOs with validation decorators | Moderate upfront | Low due to early rejection | Faster and stable | [OK] Good |