Performance: Feature modules
Feature modules affect the initial load time and memory usage by organizing code into lazy-loadable chunks.
import { Module } from '@nestjs/common'; import { UsersService } from './users.service'; import { UsersController } from './users.controller'; @Module({ providers: [UsersService], controllers: [UsersController], exports: [UsersService], }) export class UsersModule {} // Then lazy load UsersModule using dynamic imports or module federation
import { Module } from '@nestjs/common'; import { UsersService } from './users.service'; import { UsersController } from './users.controller'; @Module({ providers: [UsersService], controllers: [UsersController], exports: [UsersService], }) export class UsersModule {} // Then import UsersModule directly in AppModule eagerly
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Eagerly loading all feature modules | N/A | N/A | High initial paint delay | [X] Bad |
| Lazy loading feature modules on demand | N/A | N/A | Faster initial paint | [OK] Good |
getHello() method is called from HelloController?
@Module({
controllers: [HelloController],
providers: [HelloService],
})
export class HelloModule {}
@Injectable()
export class HelloService {
getHello() {
return 'Hello from service!';
}
}
@Controller()
export class HelloController {
constructor(private readonly helloService: HelloService) {}
@Get()
getHello() {
return this.helloService.getHello();
}
}@Module({
controllers: [UserController],
providers: [UserService],
})
export class UserModule {}
@Injectable()
export class UserService {}
@Controller()
export class UserController {
constructor(userService: UserService) {}
}AuthModule into UserModule?Pass the test and this pattern moves unknown