#Does using Interface instead of Class as DTO affect performance of Nestjs?

6 messages · Page 1 of 1 (latest)

old talon
#

The two works seamlessly and i wonder if there are underlying performance issues I don't know about

woven talon
#

Yes, interfaces cannot be reflected by the type system, and as such cannot be validated against at runtime

old talon
woven talon
#

Depends on the pipe. ValidationPipe from @nestjs/common? No, that requires classes. A custom pipe that you built? Probably

modern island
#

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.