perf: cache compiled regexes in globToRegex to avoid repeated compilation

Co-authored-by: gaomeng1900 <10131203+gaomeng1900@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-03-09 14:29:56 +00:00
parent 0b6a698f6b
commit 5852054e3a

View File

@@ -74,9 +74,16 @@ export function getFlatTree(config: DomConfig): FlatDomTree {
return elements
}
const globRegexCache = new Map<string, RegExp>()
function globToRegex(pattern: string): RegExp {
const escaped = pattern.replace(/[.+^${}()|[\]\\]/g, '\\$&')
return new RegExp(`^${escaped.replace(/\*/g, '.*')}$`)
let regex = globRegexCache.get(pattern)
if (!regex) {
const escaped = pattern.replace(/[.+^${}()|[\]\\]/g, '\\$&')
regex = new RegExp(`^${escaped.replace(/\*/g, '.*')}$`)
globRegexCache.set(pattern, regex)
}
return regex
}
function matchAttributes(