Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jest-resolve 18% performance optimization #8183

Merged
merged 2 commits into from
Mar 21, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion e2e/__tests__/__snapshots__/moduleNameMapper.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ FAIL __tests__/index.js
12 | module.exports = () => 'test';
13 |

at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:455:17)
at createNoMappedModuleFoundError (../../packages/jest-resolve/build/index.js:474:17)
at Object.require (index.js:10:1)
`;
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ FAIL __tests__/test.js
| ^
4 |

at Resolver.resolveModule (../../packages/jest-resolve/build/index.js:222:17)
at Resolver.resolveModule (../../packages/jest-resolve/build/index.js:232:17)
at Object.require (index.js:3:18)
`;
58 changes: 34 additions & 24 deletions packages/jest-resolve/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ const nodePaths = process.env.NODE_PATH
class Resolver {
private readonly _options: ResolverConfig;
private readonly _moduleMap: ModuleMap;
private readonly _moduleIDCache: {[key: string]: string};
private readonly _moduleNameCache: {[name: string]: Config.Path};
private readonly _modulePathCache: {[path: string]: Array<Config.Path>};
private readonly _moduleIDCache: Map<string, string>;
private readonly _moduleNameCache: Map<string, Config.Path>;
private readonly _modulePathCache: Map<string, Array<Config.Path>>;
private readonly _supportsNativePlatform: boolean;

constructor(moduleMap: ModuleMap, options: ResolverConfig) {
Expand All @@ -73,9 +73,9 @@ class Resolver {
? options.platforms.includes(NATIVE_PLATFORM)
: false;
this._moduleMap = moduleMap;
this._moduleIDCache = Object.create(null);
this._moduleNameCache = Object.create(null);
this._modulePathCache = Object.create(null);
this._moduleIDCache = new Map();
this._moduleNameCache = new Map();
this._modulePathCache = new Map();
}

static findNodeModule(
Expand Down Expand Up @@ -127,14 +127,16 @@ class Resolver {

// 1. If we have already resolved this module for this directory name,
// return a value from the cache.
if (this._moduleNameCache[key]) {
return this._moduleNameCache[key];
const cacheResult = this._moduleNameCache.get(key);
if (cacheResult) {
return cacheResult;
}

// 2. Check if the module is a haste module.
module = this.getModule(moduleName);
if (module) {
return (this._moduleNameCache[key] = module);
this._moduleNameCache.set(key, module);
return module;
}

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

if (module) {
return (this._moduleNameCache[key] = module);
this._moduleNameCache.set(key, module);
return module;
}
}

Expand All @@ -177,8 +180,10 @@ class Resolver {
);
// try resolving with custom resolver first to support extensions,
// then fallback to require.resolve
return (this._moduleNameCache[key] =
resolveNodeModule(module) || require.resolve(module));
const resolvedModule =
resolveNodeModule(module) || require.resolve(module);
this._moduleNameCache.set(key, resolvedModule);
return resolvedModule;
} catch (ignoredError) {}
}

Expand Down Expand Up @@ -250,16 +255,19 @@ class Resolver {
}

getModulePaths(from: Config.Path): Array<Config.Path> {
if (!this._modulePathCache[from]) {
const moduleDirectory = this._options.moduleDirectories;
const paths = nodeModulesPaths(from, {moduleDirectory});
if (paths[paths.length - 1] === undefined) {
// circumvent node-resolve bug that adds `undefined` as last item.
paths.pop();
}
this._modulePathCache[from] = paths;
const cachedModule = this._modulePathCache.get(from);
if (cachedModule) {
return cachedModule;
}

const moduleDirectory = this._options.moduleDirectories;
const paths = nodeModulesPaths(from, {moduleDirectory});
if (paths[paths.length - 1] === undefined) {
// circumvent node-resolve bug that adds `undefined` as last item.
paths.pop();
}
return this._modulePathCache[from];
this._modulePathCache.set(from, paths);
return paths;
}

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

const key = from + path.delimiter + moduleName;
if (this._moduleIDCache[key]) {
return this._moduleIDCache[key];
const cachedModuleID = this._moduleIDCache.get(key);
if (cachedModuleID) {
return cachedModuleID;
}

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

return (this._moduleIDCache[key] = id);
this._moduleIDCache.set(key, id);
return id;
}

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