Skip to content

Commit d186a23

Browse files
committed
perf: compile picomatch matcher once at Glob construction
Addresses Cursor BugBot review: cache the compiled picomatch matcher in the constructor instead of recompiling on every match() call. Matches Bun.Glob semantics where the pattern is compiled once.
1 parent 2b1032d commit d186a23

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

script/node-polyfills.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,20 +176,23 @@ const BunPolyfill = {
176176

177177
Glob: class BunGlobPolyfill {
178178
private pattern: string;
179+
/** Compiled matcher — created once at construction, reused on every match() call. */
180+
private matcher: (input: string) => boolean;
181+
179182
constructor(pattern: string) {
180183
this.pattern = pattern;
184+
// Compile once with dot:true to match Bun.Glob behavior where
185+
// `*` matches dotfiles by default (unlike picomatch defaults).
186+
this.matcher = picomatch(pattern, { dot: true });
181187
}
182188

183189
/**
184190
* Synchronously test whether a string matches the glob pattern.
185191
* Mirrors Bun.Glob.match() used by project-root detection for
186192
* language marker globs (*.sln, *.csproj, etc.).
187-
*
188-
* Uses `dot: true` to match Bun.Glob behavior where `*` matches
189-
* dotfiles by default (unlike picomatch/minimatch defaults).
190193
*/
191194
match(input: string): boolean {
192-
return picomatch(this.pattern, { dot: true })(input);
195+
return this.matcher(input);
193196
}
194197

195198
async *scan(opts?: { cwd?: string }): AsyncIterable<string> {

test/script/node-polyfills.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,12 @@ function getPicomatch() {
148148
}
149149

150150
class PolyfillGlob {
151-
private readonly pattern: string;
151+
private readonly matcher: (input: string) => boolean;
152152
constructor(pattern: string) {
153-
this.pattern = pattern;
153+
this.matcher = getPicomatch()(pattern, { dot: true });
154154
}
155155
match(input: string): boolean {
156-
return getPicomatch()(this.pattern, { dot: true })(input);
156+
return this.matcher(input);
157157
}
158158
}
159159

0 commit comments

Comments
 (0)