Unverified Commit 0593dbb2 authored by Nick Kelly's avatar Nick Kelly Committed by GitHub

remove unecessary fs.stat calls from recursive-readdir (#27769)

## Performance improvement

- [x] Performance improvement

Removes unnecessary `fs.stat` calls in `recursiveReadDir`.

`fs.readdir` had an option `withFileTypes: boolean` added NodeJS version 10. It can be used to read the directory contents and fetch its stats simultaneously, removing the need to read individual file stats after reading the directory contents.
parent 7a1c9eb1
import { promises } from 'fs'
import { Dirent, promises } from 'fs'
import { join } from 'path'
/**
......@@ -16,21 +16,19 @@ export async function recursiveReadDir(
arr: string[] = [],
rootDir: string = dir
): Promise<string[]> {
const result = await promises.readdir(dir)
const result = await promises.readdir(dir, { withFileTypes: true })
await Promise.all(
result.map(async (part: string) => {
const absolutePath = join(dir, part)
if (ignore && ignore.test(part)) return
result.map(async (part: Dirent) => {
const absolutePath = join(dir, part.name)
if (ignore && ignore.test(part.name)) return
const pathStat = await promises.stat(absolutePath)
if (pathStat.isDirectory()) {
if (part.isDirectory()) {
await recursiveReadDir(absolutePath, filter, ignore, arr, rootDir)
return
}
if (!filter.test(part)) {
if (!filter.test(part.name)) {
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