diff --git a/lib/internal/fs/glob.js b/lib/internal/fs/glob.js index 6cda2c9e6da..a0e7837db7c 100644 --- a/lib/internal/fs/glob.js +++ b/lib/internal/fs/glob.js @@ -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 = {