#Does using Interface instead of Class as DTO affect performance of Nestjs?
6 messages · Page 1 of 1 (latest)
Yes, interfaces cannot be reflected by the type system, and as such cannot be validated against at runtime
Thank you . What if I am validating using pipe, is that safe?
Depends on the pipe. ValidationPipe from @nestjs/common? No, that requires classes. A custom pipe that you built? Probably
Thank you for the clarification
Choose interfaces for simple, logic-free type checking of data structures, as they ensure type safety without affecting runtime. Opt for classes when you need to add methods, validation, or specific behaviors to your DTOs, leveraging runtime features and decorators for enhanced data handling. Essentially, use interfaces for straightforward type checks and classes for more complex scenarios requiring validation or behavior.
Interfaces offer zero runtime overhead, ideal for simple type checking without affecting performance. Classes, while providing more functionality like validation and behavior, have a minimal impact on performance due to their runtime presence. interfaces are the most performance-neutral option, and classes introduce negligible performance considerations while offering extended capabilities.