From 688f0aa873495042996a269e791d89073f7d59a0 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Wed, 30 Dec 2020 17:24:40 +0100 Subject: [PATCH] cache lookups --- packages/jest-runtime/src/index.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/packages/jest-runtime/src/index.ts b/packages/jest-runtime/src/index.ts index 04bcaff6242c..a4992f045f4f 100644 --- a/packages/jest-runtime/src/index.ts +++ b/packages/jest-runtime/src/index.ts @@ -165,7 +165,8 @@ export default class Runtime { private readonly _moduleMocker: ModuleMocker; private _isolatedModuleRegistry: ModuleRegistry | null; private _moduleRegistry: ModuleRegistry; - private readonly _esmoduleRegistry: Map; + private readonly _esmoduleRegistry: Map; + private readonly _cjsNamedExports: Map>; private readonly _testPath: Config.Path; private readonly _resolver: Resolver; private _shouldAutoMock: boolean; @@ -214,6 +215,7 @@ export default class Runtime { this._isolatedMockRegistry = null; this._moduleRegistry = new Map(); this._esmoduleRegistry = new Map(); + this._cjsNamedExports = new Map(); this._testPath = testPath; this._resolver = resolver; this._scriptTransformer = new ScriptTransformer(config, this._cacheFS); @@ -551,21 +553,29 @@ export default class Runtime { } private getExportsOfCjs(modulePath: Config.Path) { + const cachedNamedExports = this._cjsNamedExports.get(modulePath); + + if (cachedNamedExports) { + return cachedNamedExports; + } + const transformedCode = this._fileTransforms.get(modulePath)?.code ?? this.readFile(modulePath); const {exports, reexports} = parseCjs(transformedCode); - let namedExports = new Set(exports); + const namedExports = new Set(exports); reexports.forEach(reexport => { const resolved = this._resolveModule(modulePath, reexport); const exports = this.getExportsOfCjs(resolved); - namedExports = new Set([...namedExports, ...exports]); + exports.forEach(namedExports.add, namedExports); }); + this._cjsNamedExports.set(modulePath, namedExports); + return namedExports; } @@ -858,6 +868,7 @@ export default class Runtime { this._mockRegistry.clear(); this._moduleRegistry.clear(); this._esmoduleRegistry.clear(); + this._cjsNamedExports.clear(); if (this._environment) { if (this._environment.global) {