From 3bab7e8ae351f69c0b73b35abd29ab9607745129 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Thu, 11 Apr 2024 12:43:40 +0200 Subject: [PATCH] perf: cache Minimatch instance --- shared/configs.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/shared/configs.ts b/shared/configs.ts index ca6a8d9..ac375cc 100644 --- a/shared/configs.ts +++ b/shared/configs.ts @@ -1,6 +1,18 @@ -import { minimatch } from 'minimatch' +import { Minimatch } from 'minimatch' import type { FlatConfigItem, MatchedFile } from './types' +const minimatchOpts = { dot: true, matchBase: true } +const _matchInstances = new Map() + +function minimatch(file: string, pattern: string) { + let m = _matchInstances.get(pattern) + if (!m) { + m = new Minimatch(pattern, minimatchOpts) + _matchInstances.set(pattern, m) + } + return m.match(file) +} + export function getMatchedGlobs(file: string, glob: (string | string[])[]) { const globs = (Array.isArray(glob) ? glob : [glob]).flat() return globs.filter(glob => minimatch(file, glob)).flat()