Performance: Service creation
MEDIUM IMPACT
Service creation affects initial application startup time and memory usage, impacting how fast the app becomes responsive.
import { Injectable, OnModuleInit } from '@nestjs/common'; @Injectable() export class HeavyService implements OnModuleInit { private initialized = false; async onModuleInit() { // defer heavy logic asynchronously await this.lazyInit(); } private async lazyInit() { if (!this.initialized) { // heavy initialization logic this.initialized = true; } } } // In AppModule providers: [HeavyService]
import { Injectable } from '@nestjs/common'; @Injectable() export class HeavyService { constructor() { // heavy initialization logic } } // In AppModule providers: [HeavyService]
| Pattern | Startup Delay | Memory Usage | Responsiveness | Verdict |
|---|---|---|---|---|
| Eager heavy service creation | High (100+ ms) | High | Slow initial response | [X] Bad |
| Lazy asynchronous service init | Low (10-20 ms) | Moderate | Fast initial response | [OK] Good |