Unverified Commit 4b2eef46 authored by Yagiz Nizipli's avatar Yagiz Nizipli Committed by GitHub

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

This pull request replaces `fs.readdir` with `fs.opendir` for better memory usage. `fs.readdir` consumes the path and only returns value when the iteration is finished, but `fs.opendir` both returns `fs.Dirent` which is better, since we are calling an additional `fs.stat` for each iteration, but also streams the output, so that we don't store the whole array of inputs in the memory.
parent 1c2de833
...@@ -237,20 +237,16 @@ async function createTreeCodeFromPath( ...@@ -237,20 +237,16 @@ async function createTreeCodeFromPath(
} }
// We need to resolve all parallel routes in this level. // 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'] const parallelSegments: string[] = ['children']
await Promise.all( for await (const dirent of files) {
files.map(async (file) => { // Make sure name starts with "@" and is a directory.
const filePath = path.join(absoluteSegmentPath, file) if (dirent.isDirectory() && dirent.name.charCodeAt(0) === 64) {
const stat = await fs.stat(filePath) parallelSegments.push(dirent.name)
}
if (stat.isDirectory() && file.startsWith('@')) { }
parallelSegments.push(file)
}
})
)
return parallelSegments return parallelSegments
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment