mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
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:
1 parent
224d2c7886
commit
e5af022302
1 file changed
+20
-10
+20
-10
@@ -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 = {
|
||||
|
||||
Reference in new issue
Block a user