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

perf: use fs.opendir for better resource usage #52341

Merged
merged 3 commits into from
Jul 6, 2023
Merged
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
18 changes: 7 additions & 11 deletions packages/next/src/build/webpack/loaders/next-app-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,20 +237,16 @@ async function createTreeCodeFromPath(
}

// We need to resolve all parallel routes in this level.
const files = await fs.readdir(absoluteSegmentPath)
const files = await fs.opendir(absoluteSegmentPath)

const parallelSegments: string[] = ['children']

await Promise.all(
files.map(async (file) => {
const filePath = path.join(absoluteSegmentPath, file)
const stat = await fs.stat(filePath)

if (stat.isDirectory() && file.startsWith('@')) {
parallelSegments.push(file)
}
})
)
for await (const dirent of files) {
// Make sure name starts with "@" and is a directory.
if (dirent.isDirectory() && dirent.name.charCodeAt(0) === 64) {
Copy link
Member

@styfle styfle Jul 6, 2023

Choose a reason for hiding this comment

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

Is there a perf difference between startsWith() and charCodeAt()?

The PR description doesn't mention this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes there is a performance difference but it is negligible in the context of this PR. That’s why I didn’t mention it

parallelSegments.push(dirent.name)
}
}

return parallelSegments
}
Expand Down