@@ -120,10 +120,18 @@ export function detectArch(): Exclude<ArchOption, "auto"> {
120120 const ua = navigator . userAgent . toLowerCase ( ) ;
121121 if ( / a r m 6 4 | a a r c h 6 4 / . test ( ua ) ) return "arm64" ;
122122 if ( / a r m / . test ( ua ) ) return "arm" ;
123- if ( / x 8 6 / . test ( ua ) || / i 3 8 6 | i 6 8 6 / . test ( ua ) ) return "x86" ;
123+ if ( / x 8 6 (? ! _ 6 4 ) / . test ( ua ) || / i 3 8 6 | i 6 8 6 / . test ( ua ) ) return "x86" ;
124124 return "x64" ;
125125}
126126
127+ function resolveOs ( os : OsOption ) : Exclude < OsOption , "auto" > {
128+ return os === "auto" ? detectPlatform ( ) : os ;
129+ }
130+
131+ function resolveArch ( arch : ArchOption ) : Exclude < ArchOption , "auto" > {
132+ return arch === "auto" ? detectArch ( ) : arch ;
133+ }
134+
127135export function getSelectedArch ( ) : ArchOption {
128136 const saved = GM_getValue ( ARCH_STORAGE_KEY , "auto" ) ;
129137 if ( ARCH_OPTIONS . includes ( saved as ArchOption ) ) return saved as ArchOption ;
@@ -144,15 +152,22 @@ export function setSelectedPkg(pkg: PkgOption): void {
144152 GM_setValue ( PKG_STORAGE_KEY , pkg ) ;
145153}
146154
155+ let keywordCache : Set < string > | null = null ;
156+ let keywordCacheKey = "" ;
157+
158+ const regexCache = new Map < string , RegExp > ( ) ;
159+
147160function buildMatchKeywords ( ) : Set < string > {
148161 const os = getSelectedOs ( ) ;
149162 const arch = getSelectedArch ( ) ;
150163 const pkg = getSelectedPkg ( ) ;
164+ const cacheKey = `${ os } :${ arch } :${ pkg } ` ;
165+ if ( keywordCache && keywordCacheKey === cacheKey ) return keywordCache ;
151166
152- const resolvedOs = os === "auto" ? detectPlatform ( ) : os ;
167+ const resolvedOs = resolveOs ( os ) ;
153168 const keywords = new Set ( SYSTEM_KEYWORDS [ resolvedOs ] ) ;
154169
155- const resolvedArch = arch === "auto" ? detectArch ( ) : arch ;
170+ const resolvedArch = resolveArch ( arch ) ;
156171 for ( const kw of ARCH_KEYWORDS [ resolvedArch ] ) {
157172 keywords . add ( kw ) ;
158173 }
@@ -163,6 +178,8 @@ function buildMatchKeywords(): Set<string> {
163178 for ( const kw of PKG_KEYWORDS . portable [ resolvedOs ] ) keywords . add ( kw ) ;
164179 }
165180
181+ keywordCache = keywords ;
182+ keywordCacheKey = cacheKey ;
166183 return keywords ;
167184}
168185
@@ -171,15 +188,15 @@ export function getKeywordsPreview(
171188 arch : ArchOption ,
172189 pkg : PkgOption ,
173190) : string [ ] {
174- const resolvedOs = os === "auto" ? detectPlatform ( ) : os ;
175- const resolvedArch = arch === "auto" ? detectArch ( ) : arch ;
191+ const resolvedOs = resolveOs ( os ) ;
192+ const resolvedArch = resolveArch ( arch ) ;
176193
177194 const result = [
178195 ...SYSTEM_KEYWORDS [ resolvedOs ] ,
179196 ...ARCH_KEYWORDS [ resolvedArch ] ,
197+ ...PKG_KEYWORDS . installer [ resolvedOs ] ,
180198 ] ;
181199
182- result . push ( ...PKG_KEYWORDS . installer [ resolvedOs ] ) ;
183200 if ( pkg === "all" ) {
184201 result . push ( ...PKG_KEYWORDS . portable [ resolvedOs ] ) ;
185202 } else {
@@ -211,9 +228,12 @@ function extractFilename(anchor: HTMLAnchorElement): string | null {
211228function countMatches ( filename : string , keywords : Set < string > ) : number {
212229 let count = 0 ;
213230 for ( const keyword of keywords ) {
214- if ( new RegExp ( `(^|[^a-z0-9])${ keyword } ([^a-z0-9]|$)` ) . test ( filename ) ) {
215- count ++ ;
231+ let re = regexCache . get ( keyword ) ;
232+ if ( ! re ) {
233+ re = new RegExp ( `(^|[^a-z0-9])${ keyword } ([^a-z0-9]|$)` ) ;
234+ regexCache . set ( keyword , re ) ;
216235 }
236+ if ( re . test ( filename ) ) count ++ ;
217237 }
218238 return count ;
219239}
0 commit comments