Skip to content

Commit

Permalink
perf: use fs.readdirSync with withFileTypes (#52340)
Browse files Browse the repository at this point in the history
There is no need to make a `fs.statSync` call for each file while reading a directory, since `fs.readdirSync` supports `withFileTypes` property that returns a `fs.Dirent` which includes the necessary information for detecting if the particular item is directory or not.
  • Loading branch information
anonrig authored Jul 6, 2023
1 parent f0fc83b commit 1fbb513
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions packages/next/src/server/lib/recursive-readdir-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ export function recursiveReadDirSync(
/** Used to replace the initial path, only the relative path is left, it's faster than path.relative. */
rootDir = dir
): string[] {
const result = fs.readdirSync(dir)
const result = fs.readdirSync(dir, { withFileTypes: true })

result.forEach((part: string) => {
const absolutePath = join(dir, part)
const pathStat = fs.statSync(absolutePath)
result.forEach((part) => {
const absolutePath = join(dir, part.name)

if (pathStat.isDirectory()) {
if (part.isDirectory()) {
recursiveReadDirSync(absolutePath, arr, rootDir)
return
}
Expand Down

0 comments on commit 1fbb513

Please sign in to comment.