Unverified Commit 1fbb5138 authored by Yagiz Nizipli's avatar Yagiz Nizipli Committed by GitHub

perf: use fs.readdirSync with withFileTypes (#52340)

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.
parent f0fc83b4
...@@ -13,13 +13,12 @@ export function recursiveReadDirSync( ...@@ -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. */ /** Used to replace the initial path, only the relative path is left, it's faster than path.relative. */
rootDir = dir rootDir = dir
): string[] { ): string[] {
const result = fs.readdirSync(dir) const result = fs.readdirSync(dir, { withFileTypes: true })
result.forEach((part: string) => { result.forEach((part) => {
const absolutePath = join(dir, part) const absolutePath = join(dir, part.name)
const pathStat = fs.statSync(absolutePath)
if (pathStat.isDirectory()) { if (part.isDirectory()) {
recursiveReadDirSync(absolutePath, arr, rootDir) recursiveReadDirSync(absolutePath, arr, rootDir)
return return
} }
......
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