Skip to content
Open
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');
}
31 changes: 21 additions & 10 deletions lib/internal/fs/glob.js
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,8 @@ class Glob {
}
}

let patternFnCache;

/**
* Check if a path matches a glob pattern
* @param {string} path the path to check
Expand All @@ -932,16 +934,25 @@ 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,
});

patternFnCache ??= new SafeMap();

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm not sure if it's worth it to delay the map instantiation like this, but did it just in case.


let matcher;
if (patternFnCache.has(pattern)) {
matcher = patternFnCache.get(pattern);
} else {
matcher = createMatcher(pattern, {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i re-used createMatcher from above as i saw that it uses the same default options as the previous lines did.

i thought about putting the caching in that function instead, but then we would need to base the caching on all the options it accepts, so i decided to keep it here.

kEmptyObject,
platform: windows ? 'win32' : 'posix',
});
patternFnCache.set(pattern, matcher);

if (patternFnCache.size >= 250) {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

randomly picked number, i'm not sure how to find a "good" value though.

patternFnCache.delete(patternFnCache.keys().next().value);
}
}

return matcher.match(path);
}

module.exports = {
Expand Down
Loading