Unverified Commit d175aea2 authored by Shu Ding's avatar Shu Ding Committed by GitHub

perf: Refactor recursiveReadDirSync (#52517)

- Use `opendirSync` instead of `readdirSync`
- Use `.slice` instead of `.replace` as the rootDir

Before (1.54ms):

![CleanShot 2023-07-10 at 18 33 23@2x](https://github.com/vercel/next.js/assets/3676859/829e0a49-35da-4754-bc3f-6fe243e815f4)

With the change (0.88ms):

![CleanShot 2023-07-10 at 18 47 43@2x](https://github.com/vercel/next.js/assets/3676859/cabe1fdd-5861-49ba-8c8d-b505c16fbf7a)

This scales when public/ folder grows.
parent 0ce55e3f
import fs from 'fs'
import { join } from 'path'
import { sep } from 'path'
/**
* Recursively read directory
......@@ -10,20 +10,22 @@ export function recursiveReadDirSync(
dir: string,
/** This doesn't have to be provided, it's used for the recursion */
arr: string[] = [],
/** Used to replace the initial path, only the relative path is left, it's faster than path.relative. */
rootDir = dir
/** Used to remove the initial path suffix and leave only the relative, faster than path.relative. */
rootDirLength = dir.length
): string[] {
const result = fs.readdirSync(dir, { withFileTypes: true })
result.forEach((part) => {
const absolutePath = join(dir, part.name)
// Use opendirSync for better memory usage
const result = fs.opendirSync(dir)
let part: fs.Dirent | null
while ((part = result.readSync())) {
const absolutePath = dir + sep + part.name
if (part.isDirectory()) {
recursiveReadDirSync(absolutePath, arr, rootDir)
return
recursiveReadDirSync(absolutePath, arr, rootDirLength)
} else {
arr.push(absolutePath.slice(rootDirLength))
}
arr.push(absolutePath.replace(rootDir, ''))
})
}
result.closeSync()
return arr
}
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