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-haste-map] reduce the number of lstat calls in node crawler #9514

Merged
merged 9 commits into from Feb 5, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -21,6 +21,8 @@

### Performance

- `[jest-haste-map]` Reduce number of `lstat` calls in node crawler ([#9514](https://github.com/facebook/jest/pull/9514))

## 25.1.0

### Features
Expand Down
31 changes: 26 additions & 5 deletions packages/jest-haste-map/src/crawlers/node.ts
Expand Up @@ -32,22 +32,42 @@ function find(

function search(directory: string): void {
activeCalls++;
fs.readdir(directory, (err, names) => {
fs.readdir(directory, {withFileTypes: true}, (err, entries) => {
activeCalls--;
if (err) {
callback(result);
return;
}
names.forEach(file => {
file = path.join(directory, file);
if (ignore(file)) {
return;
entries.forEach((entry: string | fs.Dirent) => {
let file: string;
// node < v10.10 does not support the withFileTypes option, and
// entry will be a string.
if (typeof entry === 'string') {
file = path.join(directory, entry);
} else {
file = path.join(directory, entry.name);

if (ignore(file)) {
SimenB marked this conversation as resolved.
Show resolved Hide resolved
return;
}

if (entry.isSymbolicLink()) {
return;
}

if (entry.isDirectory()) {
search(file);
return;
}
}
SimenB marked this conversation as resolved.
Show resolved Hide resolved

activeCalls++;

fs.lstat(file, (err, stat) => {
activeCalls--;

// This logic is unnecessary for node > v10.10, but leaving it in
// since we need it for backwards-compatibility still.
Comment on lines +69 to +70
Copy link
Member

@SimenB SimenB Feb 5, 2022

Choose a reason for hiding this comment

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

@yepitschunked we're about to drop node 10 (#12220), which part of the logic does this refer to? The entire stat or just specific parts of it?

Copy link
Member

Choose a reason for hiding this comment

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

this PR is old, but would love your comment over in that PR 🙂

if (!err && stat && !stat.isSymbolicLink()) {
if (stat.isDirectory()) {
search(file);
Expand All @@ -58,6 +78,7 @@ function find(
}
}
}

if (activeCalls === 0) {
callback(result);
}
Expand Down