diff --git a/packages/core/injector/module-token-factory.ts b/packages/core/injector/module-token-factory.ts index 4a407885c6f..f26bc42d943 100644 --- a/packages/core/injector/module-token-factory.ts +++ b/packages/core/injector/module-token-factory.ts @@ -1,9 +1,10 @@ -import { DynamicModule } from '@nestjs/common'; +import { DynamicModule, Logger } from '@nestjs/common'; import { Type } from '@nestjs/common/interfaces/type.interface'; import { randomStringGenerator } from '@nestjs/common/utils/random-string-generator.util'; import { isFunction, isSymbol } from '@nestjs/common/utils/shared.utils'; import { createHash } from 'crypto'; import stringify from 'fast-safe-stringify'; +import { performance } from 'perf_hooks'; const CLASS_STR = 'class '; const CLASS_STR_LEN = CLASS_STR.length; @@ -11,6 +12,9 @@ const CLASS_STR_LEN = CLASS_STR.length; export class ModuleTokenFactory { private readonly moduleTokenCache = new Map(); private readonly moduleIdsCache = new WeakMap, string>(); + private readonly logger = new Logger(ModuleTokenFactory.name, { + timestamp: true, + }); public create( metatype: Type, @@ -26,7 +30,17 @@ export class ModuleTokenFactory { module: this.getModuleName(metatype), dynamic: dynamicModuleMetadata, }; + const start = performance.now(); const opaqueTokenString = this.getStringifiedOpaqueToken(opaqueToken); + const timeSpentInMs = performance.now() - start; + + if (timeSpentInMs > 10) { + this.logger.warn( + `The module "${opaqueToken.module}" is taking ${timeSpentInMs.toFixed( + 2, + )}ms to serialize, this may be caused by larger objects assigned to the module. More details: https://github.com/nestjs/nest/issues/12738`, + ); + } return this.hashString(opaqueTokenString); }