Performance: Injectable decorator
This affects the dependency injection system's initialization and service instantiation speed, impacting app startup and runtime responsiveness.
@Injectable() class UserRepository {} @Injectable() class UserService { constructor(private readonly repo: UserRepository) {} }
class UserService { constructor() { this.repo = new UserRepository(); } } class UserRepository {}
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual new calls for dependencies | N/A | N/A | N/A | [X] Bad |
| Using @Injectable with DI container | N/A | N/A | N/A | [OK] Good |
@Injectable() decorator in NestJS?@Injectable()@Controller() or database decorators serve different purposes, not dependency injection.@Injectable() = marks class for DI [OK]@Injectable() means NestJS manages the class [OK]@Injectable() with @Controller()@Injectable() is correct.@Injectable.@Injectable() [OK]@Injectable@Injectable without decorator syntaxappService.getMessage() is called?import { Injectable } from '@nestjs/common';
@Injectable()
class AppService {
getMessage() {
return 'Hello from AppService';
}
}
const appService = new AppService();
console.log(appService.getMessage());@Injectable() effect on class instantiation@Injectable() decorator marks the class for NestJS DI but does not change normal instantiation behavior.getMessage(), which returns the string as expected.import { Injectable } from '@nestjs/common';
@Injectable
export class UserService {
getUser() {
return 'User data';
}
}@Injectable decorator must be called with parentheses: @Injectable().@Injectable decorator -> Option A@Injectable() [OK]@Injectable() allows NestJS to manage the logging service lifecycle and inject it automatically where needed.@Controller() is for routing, not services.@Injectable() on the logging class and inject it where needed -> Option B@Injectable() for reusable NestJS services [OK]@Injectable() and instantiating manually@Controller() for servicesPass the test and this pattern moves unknown