fix(providence): [optimised-glob] use Array.concat for memory efficiency

This commit is contained in:
Thijs Louisse 2025-01-22 17:23:01 +01:00 committed by Thijs Louisse
parent a1d6dd90a8
commit 0f0991cd62
2 changed files with 17 additions and 7 deletions

View file

@ -0,0 +1,5 @@
---
'providence-analytics': patch
---
[optimised-glob] use Array.concat for memory efficiency

View file

@ -253,12 +253,16 @@ const getAllDirentsFromStartPath = memoize(
return dirents;
}
dirents.push(
// @ts-expect-error
...(await fs.promises.readdir(startPath, { withFileTypes: true, recursive: true })),
const direntResult = /** @type {* & DirentWithPath[]} */ (
await fs.promises.readdir(startPath, {
withFileTypes: true,
// @ts-expect-error
recursive: true,
})
);
cache[startPath] = dirents;
return dirents;
cache[startPath] = direntResult;
return direntResult;
},
);
@ -401,7 +405,7 @@ export async function optimisedGlob(globOrGlobs, providedOptions = {}) {
/** @type {RegExp[]} */
const matchRegexes = [];
/** @type {{dirent:nodeFs.Dirent;relativeToCwdPath:string}[]} */
const globEntries = [];
let globEntries = [];
for (const glob of globs) {
const isNegative = glob.startsWith('!');
@ -431,7 +435,8 @@ export async function optimisedGlob(globOrGlobs, providedOptions = {}) {
cwd,
});
globEntries.push(...allDirEntsRelativeToCwd);
// N.B. `globEntries.push(...allDirEntsRelativeToCwd);` can give memory issues for large file trees
globEntries = globEntries.concat(allDirEntsRelativeToCwd);
} catch (e) {
if (!options.suppressErrors) {
throw e;