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

Cache checked files in fs #8388

Merged
merged 6 commits into from May 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -37,6 +37,7 @@
### Performance

- `[jest-runtime]` Fix module registry memory leak ([#8282](https://github.com/facebook/jest/pull/8282))
- `[jest-resolve]` optimize resolve module path ([#8388](https://github.com/facebook/jest/pull/8388))

## 24.7.1

Expand Down
57 changes: 34 additions & 23 deletions packages/jest-resolve/src/defaultResolver.ts
Expand Up @@ -142,39 +142,50 @@ function resolveSync(
}
}

/*
* helper functions
*/
function isFile(file: Config.Path): boolean {
let result;
enum IPathType {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a big deal, but FYI-- not using the "prefix with I" convention in the Jest codebase.

FILE = 1,
DIRECTORY = 2,
OTHER = 3,
}
const checkedPaths = new Map<string, IPathType>();
function statSyncCached(path: string): number {
const result = checkedPaths.get(path);
if (result !== undefined) {
return result;
}

let stat;
try {
const stat = fs.statSync(file);
result = stat.isFile() || stat.isFIFO();
stat = fs.statSync(path);
} catch (e) {
if (!(e && e.code === 'ENOENT')) {
if (!(e && (e.code === 'ENOENT' || e.code === 'ENOTDIR'))) {
throw e;
}
result = false;
}

return result;
}

function isDirectory(dir: Config.Path): boolean {
let result;

try {
const stat = fs.statSync(dir);
result = stat.isDirectory();
} catch (e) {
if (!(e && (e.code === 'ENOENT' || e.code === 'ENOTDIR'))) {
throw e;
if (stat) {
if (stat.isFile() || stat.isFIFO()) {
checkedPaths.set(path, IPathType.FILE);
return IPathType.FILE;
} else if (stat.isDirectory()) {
checkedPaths.set(path, IPathType.DIRECTORY);
return IPathType.DIRECTORY;
}
result = false;
}

return result;
checkedPaths.set(path, IPathType.OTHER);
return IPathType.OTHER;
}

/*
* helper functions
*/
function isFile(file: Config.Path): boolean {
return statSyncCached(file) === IPathType.FILE;
}

function isDirectory(dir: Config.Path): boolean {
return statSyncCached(dir) === IPathType.DIRECTORY;
}

function isCurrentDirectory(testPath: Config.Path): boolean {
Expand Down