@@ -50,6 +50,22 @@ function getFieldWords(field: string): { words: string[]; acronym: string } {
5050 return entry ;
5151}
5252
53+ const normalizedFieldCache = new Map < string , string > ( ) ;
54+
55+ /** Trims and lowercases a field once. Every row is scored again on the next keystroke. */
56+ function normalizeField ( rawField : string | null | undefined ) : string {
57+ if ( ! rawField ) return '' ;
58+ const cached = normalizedFieldCache . get ( rawField ) ;
59+ if ( cached !== undefined ) return cached ;
60+ const normalized = rawField . trim ( ) . toLocaleLowerCase ( ) ;
61+ // Empty results are not worth a cache slot: whitespace padding is free to re-trim.
62+ if ( normalized ) {
63+ if ( normalizedFieldCache . size > 1024 ) normalizedFieldCache . clear ( ) ;
64+ normalizedFieldCache . set ( rawField , normalized ) ;
65+ }
66+ return normalized ;
67+ }
68+
5369function scoreSingleTerm ( query : string , field : string ) : number {
5470 if ( field === query ) return 100 ;
5571 if ( field . startsWith ( query ) ) return 94 ;
@@ -76,11 +92,13 @@ export function scoreTextMatch(query: string, fields: Array<string | null | unde
7692 const multiToken = tokens . length > 1 ;
7793
7894 let best = 0 ;
79- const normalizedFields : string [ ] = [ ] ;
95+ let normalizedFields : string [ ] | undefined ;
8096 for ( const rawField of fields ) {
81- const field = rawField ?. trim ( ) . toLocaleLowerCase ( ) ;
97+ const field = normalizeField ( rawField ) ;
8298 if ( ! field ) continue ;
83- normalizedFields . push ( field ) ;
99+ // Only the multi-token branches read this list, so single-token queries (the common case)
100+ // no longer allocate an array per row.
101+ if ( multiToken ) ( normalizedFields ??= [ ] ) . push ( field ) ;
84102 best = Math . max ( best , scoreSingleTerm ( normalizedQuery , field ) ) ;
85103
86104 if ( multiToken && tokens . every ( token => field . includes ( token ) ) ) {
@@ -90,7 +108,7 @@ export function scoreTextMatch(query: string, fields: Array<string | null | unde
90108
91109 if (
92110 multiToken
93- && tokens . every ( token => normalizedFields . some ( field => field . includes ( token ) ) )
111+ && tokens . every ( token => ( normalizedFields ?? [ ] ) . some ( field => field . includes ( token ) ) )
94112 ) {
95113 best = Math . max ( best , 72 ) ;
96114 }
@@ -100,11 +118,17 @@ export function scoreTextMatch(query: string, fields: Array<string | null | unde
100118 // evidence that accumulates instead of an all-or-nothing AND expression.
101119 // Exact/full-query matches above still outrank partial token coverage.
102120 if ( multiToken ) {
103- const tokenScores = tokens . map ( token => Math . max (
104- 0 ,
105- ...normalizedFields . map ( field => scoreSingleTerm ( token , field ) ) ,
106- ) ) ;
107- const matchedScores = tokenScores . filter ( score => score > 0 ) ;
121+ const matchedScores : number [ ] = [ ] ;
122+ for ( const token of tokens ) {
123+ let strongest = 0 ;
124+ // A loop instead of Math.max(...fields.map(...)): the spread allocates an argument list
125+ // per token, and this runs once per candidate row per keystroke.
126+ for ( const field of normalizedFields ?? [ ] ) {
127+ const score = scoreSingleTerm ( token , field ) ;
128+ if ( score > strongest ) strongest = score ;
129+ }
130+ if ( strongest > 0 ) matchedScores . push ( strongest ) ;
131+ }
108132 if ( matchedScores . length > 0 ) {
109133 const coverage = matchedScores . length / tokens . length ;
110134 const strongest = Math . max ( ...matchedScores ) ;
0 commit comments