Skip to content

Commit 800e6fb

Browse files
jest-resolve 18% performance optimization (#8183)
* jest-resolve performance optimizations in hot spot. * Update CHANGELOG.md
1 parent 4a49aeb commit 800e6fb

File tree

4 files changed

+37
-26
lines changed

4 files changed

+37
-26
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
- `[jest-haste-map]` Optimize haste map data structure for serialization/deserialization ([#8171](https://github.com/facebook/jest/pull/8171))
3636
- `[jest-haste-map]` Avoid persisting haste map or processing files when not changed ([#8153](https://github.com/facebook/jest/pull/8153))
37+
- `[jest-resolve]` Optimize internal cache lookup performance ([#8183](https://github.com/facebook/jest/pull/8183))
3738

3839
## 24.5.0
3940

e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ FAIL __tests__/index.js
3030
12 | module.exports = () => 'test';
3131
13 |
3232
33-
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:455:17)
33+
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:474:17)
3434
at Object.require (index.js:10:1)
3535
`;

e2e/__tests__/__snapshots__/resolveNoFileExtensions.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ FAIL __tests__/test.js
3333
| ^
3434
4 |
3535
36-
at Resolver.resolveModule (../../packages/jest-resolve/build/index.js:222:17)
36+
at Resolver.resolveModule (../../packages/jest-resolve/build/index.js:232:17)
3737
at Object.require (index.js:3:18)
3838
`;

packages/jest-resolve/src/index.ts

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ const nodePaths = process.env.NODE_PATH
5050
class Resolver {
5151
private readonly _options: ResolverConfig;
5252
private readonly _moduleMap: ModuleMap;
53-
private readonly _moduleIDCache: {[key: string]: string};
54-
private readonly _moduleNameCache: {[name: string]: Config.Path};
55-
private readonly _modulePathCache: {[path: string]: Array<Config.Path>};
53+
private readonly _moduleIDCache: Map<string, string>;
54+
private readonly _moduleNameCache: Map<string, Config.Path>;
55+
private readonly _modulePathCache: Map<string, Array<Config.Path>>;
5656
private readonly _supportsNativePlatform: boolean;
5757

5858
constructor(moduleMap: ModuleMap, options: ResolverConfig) {
@@ -73,9 +73,9 @@ class Resolver {
7373
? options.platforms.includes(NATIVE_PLATFORM)
7474
: false;
7575
this._moduleMap = moduleMap;
76-
this._moduleIDCache = Object.create(null);
77-
this._moduleNameCache = Object.create(null);
78-
this._modulePathCache = Object.create(null);
76+
this._moduleIDCache = new Map();
77+
this._moduleNameCache = new Map();
78+
this._modulePathCache = new Map();
7979
}
8080

8181
static findNodeModule(
@@ -127,14 +127,16 @@ class Resolver {
127127

128128
// 1. If we have already resolved this module for this directory name,
129129
// return a value from the cache.
130-
if (this._moduleNameCache[key]) {
131-
return this._moduleNameCache[key];
130+
const cacheResult = this._moduleNameCache.get(key);
131+
if (cacheResult) {
132+
return cacheResult;
132133
}
133134

134135
// 2. Check if the module is a haste module.
135136
module = this.getModule(moduleName);
136137
if (module) {
137-
return (this._moduleNameCache[key] = module);
138+
this._moduleNameCache.set(key, module);
139+
return module;
138140
}
139141

140142
// 3. Check if the module is a node module and resolve it based on
@@ -161,7 +163,8 @@ class Resolver {
161163
module = resolveNodeModule(moduleName);
162164

163165
if (module) {
164-
return (this._moduleNameCache[key] = module);
166+
this._moduleNameCache.set(key, module);
167+
return module;
165168
}
166169
}
167170

@@ -177,8 +180,10 @@ class Resolver {
177180
);
178181
// try resolving with custom resolver first to support extensions,
179182
// then fallback to require.resolve
180-
return (this._moduleNameCache[key] =
181-
resolveNodeModule(module) || require.resolve(module));
183+
const resolvedModule =
184+
resolveNodeModule(module) || require.resolve(module);
185+
this._moduleNameCache.set(key, resolvedModule);
186+
return resolvedModule;
182187
} catch (ignoredError) {}
183188
}
184189

@@ -250,16 +255,19 @@ class Resolver {
250255
}
251256

252257
getModulePaths(from: Config.Path): Array<Config.Path> {
253-
if (!this._modulePathCache[from]) {
254-
const moduleDirectory = this._options.moduleDirectories;
255-
const paths = nodeModulesPaths(from, {moduleDirectory});
256-
if (paths[paths.length - 1] === undefined) {
257-
// circumvent node-resolve bug that adds `undefined` as last item.
258-
paths.pop();
259-
}
260-
this._modulePathCache[from] = paths;
258+
const cachedModule = this._modulePathCache.get(from);
259+
if (cachedModule) {
260+
return cachedModule;
261+
}
262+
263+
const moduleDirectory = this._options.moduleDirectories;
264+
const paths = nodeModulesPaths(from, {moduleDirectory});
265+
if (paths[paths.length - 1] === undefined) {
266+
// circumvent node-resolve bug that adds `undefined` as last item.
267+
paths.pop();
261268
}
262-
return this._modulePathCache[from];
269+
this._modulePathCache.set(from, paths);
270+
return paths;
263271
}
264272

265273
getModuleID(
@@ -270,8 +278,9 @@ class Resolver {
270278
const moduleName = _moduleName || '';
271279

272280
const key = from + path.delimiter + moduleName;
273-
if (this._moduleIDCache[key]) {
274-
return this._moduleIDCache[key];
281+
const cachedModuleID = this._moduleIDCache.get(key);
282+
if (cachedModuleID) {
283+
return cachedModuleID;
275284
}
276285

277286
const moduleType = this._getModuleType(moduleName);
@@ -285,7 +294,8 @@ class Resolver {
285294
(absolutePath ? absolutePath + sep : '') +
286295
(mockPath ? mockPath + sep : '');
287296

288-
return (this._moduleIDCache[key] = id);
297+
this._moduleIDCache.set(key, id);
298+
return id;
289299
}
290300

291301
private _getModuleType(moduleName: string): 'node' | 'user' {

0 commit comments

Comments
 (0)