Skip to content

Commit

Permalink
Cache checked files in fs (#8388)
Browse files Browse the repository at this point in the history
* Cache checked files in fs

* Cache checked dirs in fs

* Refactoring isfile

* Add to change.md

* Use unem instead of const

* Update changelog
  • Loading branch information
Connormiha authored and scotthovestadt committed May 1, 2019
1 parent 8329972 commit a9c0e6b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
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 {
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

0 comments on commit a9c0e6b

Please sign in to comment.