Back to all reviewers

Use factory providers

nestjs/nest
Based on 3 comments
TypeScript

Prefer `useFactory` over `useValue` for better performance in NestJS dependency injection, especially for large objects or complex configurations. Using `useValue` with large objects can significantly slow down application bootstrapping due to expensive module serialization.

NestJS TypeScript

Reviewer Prompt

Prefer useFactory over useValue for better performance in NestJS dependency injection, especially for large objects or complex configurations. Using useValue with large objects can significantly slow down application bootstrapping due to expensive module serialization.

When defining providers in a module, implement them as factory functions:

// Less optimal - may cause performance issues with large objects
{ 
  provide: MULTER_MODULE_OPTIONS, 
  useValue: complexConfigObject 
}

// Better performance
{ 
  provide: MULTER_MODULE_OPTIONS, 
  useFactory: () => complexConfigObject 
}

This optimization prevents deep serialization of object structures during module token creation. The serialization process in NestJS needs to stringify providers to create unique identifiers, and complex objects can significantly impact performance.

If your application has slow startup times, check for warnings about module serialization exceeding 10ms, which indicates potential performance bottlenecks. Converting value providers to factory providers can substantially reduce initialization time in larger applications with complex dependency graphs.

3
Comments Analyzed
TypeScript
Primary Language
NestJS
Category

Source Discussions