Skip to content

Commit f87f628

Browse files
committed
fs: add pattern cache for matchGlobPattern()
Signed-off-by: Adam Haglund <adam@haglund.dev>
1 parent 137b97c commit f87f628

1 file changed

Lines changed: 20 additions & 10 deletions

File tree

lib/internal/fs/glob.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,9 @@ class Glob {
922922
}
923923
}
924924

925+
const kGlobPatternCacheLimit = 250;
926+
const patternFnCache = new SafeMap();
927+
925928
/**
926929
* Check if a path matches a glob pattern
927930
* @param {string} path the path to check
@@ -932,16 +935,23 @@ class Glob {
932935
function matchGlobPattern(path, pattern, windows = isWindows) {
933936
validateString(path, 'path');
934937
validateString(pattern, 'pattern');
935-
return lazyMinimatch().minimatch(path, pattern, {
936-
kEmptyObject,
937-
nocase: isMacOS || isWindows,
938-
windowsPathsNoEscape: true,
939-
nonegate: true,
940-
nocomment: true,
941-
optimizationLevel: 2,
942-
platform: windows ? 'win32' : 'posix',
943-
nocaseMagicOnly: true,
944-
});
938+
939+
let matcher;
940+
if (patternFnCache.has(pattern)) {
941+
matcher = patternFnCache.get(pattern);
942+
} else {
943+
matcher = createMatcher(pattern, {
944+
kEmptyObject,
945+
platform: windows ? 'win32' : 'posix',
946+
});
947+
patternFnCache.set(pattern, matcher);
948+
949+
if (patternFnCache.size >= kGlobPatternCacheLimit) {
950+
patternFnCache.delete(patternFnCache.keys().next().value);
951+
}
952+
}
953+
954+
return matcher.match(path);
945955
}
946956

947957
module.exports = {

0 commit comments

Comments
 (0)