Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions benchmark/path/matchesGlob-posix.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';
const common = require('../common.js');
const { posix } = require('path');
const assert = require('assert');

const bench = common.createBenchmark(main, {
path: [
'src/index.ts',
'src/index.test.ts',
'src/foo/bar/biz/baz/index.test.ts',
],
pattern: [
'src/**/baz/*.test.ts',
'src/**/baz/*.ts',
'test/**/*.ts',
],
n: [1e5],
});

function main({ path, pattern, n }) {
bench.start();
let a;
for (let i = 0; i < n; i++) {
a = posix.matchesGlob(path, pattern);
}
bench.end(n);
assert(a + 'a');
}
29 changes: 29 additions & 0 deletions benchmark/path/matchesGlob-win32.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';
const common = require('../common.js');
const { win32 } = require('path');
const assert = require('assert');

const bench = common.createBenchmark(main, {
path: [
'src/index.ts',
'src\\index.ts',
'src/foo/bar/biz/baz/index.test.ts',
'src\\foo\\bar\\biz\\baz\\index.test.ts',
],
pattern: [
'src/**/baz/*.test.ts',
'src/**/baz/*.ts',
'test/**/*.ts',
],
n: [1e5],
});

function main({ path, pattern, n }) {
bench.start();
let a;
for (let i = 0; i < n; i++) {
a = win32.matchesGlob(path, pattern);
}
bench.end(n);
assert(a + 'a');
}
30 changes: 20 additions & 10 deletions lib/internal/fs/glob.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = {
Expand Down
Loading