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(
}
// 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) {
parallelSegments.push(dirent.name)
}
}
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