fs: add pattern cache for matchGlobPattern()

Signed-off-by: Adam Haglund <adam@haglund.dev>
PR-URL: https://github.com/nodejs/node/pull/63915
Reviewed-By: Aviv Keller <me@aviv.sh>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
bq authored and Antoine du Hamel committed 2026-08-06 15:35:08 +02:00
1 parent 224d2c7886
commit e5af022302
1 file changed
+20 -10
+20 -10
View File
@@ -922,6 +922,9 @@ class Glob {
}
}
const kGlobPatternCacheLimit = 250;
const patternFnCache = new SafeMap();
/**
* Check if a path matches a glob pattern
* @param {string} path the path to check
@@ -932,16 +935,23 @@ class Glob {
function matchGlobPattern(path, pattern, windows = isWindows) {
validateString(path, 'path');
validateString(pattern, 'pattern');
return lazyMinimatch().minimatch(path, pattern, {
kEmptyObject,
nocase: isMacOS || isWindows,
windowsPathsNoEscape: true,
nonegate: true,
nocomment: true,
optimizationLevel: 2,
platform: windows ? 'win32' : 'posix',
nocaseMagicOnly: true,
});
let matcher;
if (patternFnCache.has(pattern)) {
matcher = patternFnCache.get(pattern);
} else {
matcher = createMatcher(pattern, {
kEmptyObject,
platform: windows ? 'win32' : 'posix',
});
patternFnCache.set(pattern, matcher);
if (patternFnCache.size >= kGlobPatternCacheLimit) {
patternFnCache.delete(patternFnCache.keys().next().value);
}
}
return matcher.match(path);
}
module.exports = {