@@ -238,55 +238,73 @@ function getQuickFilterTexts(row: GridRowModel, columns: GridColumnLookup | unde
238238
239239// Search texts per column lookup and row object. The grid keeps one lookup while the columns and
240240// hidden columns stay the same, so typing in the quick filter reads every cell once, not per keystroke.
241- //
242- // A row's texts are stored joined by TEXT_SEPARATOR, one string per row instead of an array of a
243- // dozen, so the first keystroke allocates less and a term is one `includes` per row. A term that
244- // contains the separator could match across two texts, so such terms search the split texts; a
245- // row whose own text contains the separator keeps its array.
246- const TEXT_SEPARATOR = '\u0000' ;
247- type QuickFilterTexts = string | string [ ] ;
248- const quickFilterTextCache = new WeakMap < GridColumnLookup , WeakMap < GridRowModel , QuickFilterTexts > > ( ) ;
249-
250- function packQuickFilterTexts ( texts : string [ ] ) : QuickFilterTexts {
251- for ( const text of texts ) if ( text . includes ( TEXT_SEPARATOR ) ) return texts ;
252- return texts . join ( TEXT_SEPARATOR ) ;
253- }
241+ const quickFilterTextCache = new WeakMap < GridColumnLookup , WeakMap < GridRowModel , string [ ] > > ( ) ;
254242
255- function getCachedQuickFilterTexts ( row : GridRowModel , columns : GridColumnLookup | undefined ) : QuickFilterTexts {
243+ function getCachedQuickFilterTexts ( row : GridRowModel , columns : GridColumnLookup | undefined ) : string [ ] {
256244 if ( ! columns ) return getQuickFilterTexts ( row , columns ) ;
257245 let byRow = quickFilterTextCache . get ( columns ) ;
258246 if ( ! byRow ) {
259247 byRow = new WeakMap ( ) ;
260248 quickFilterTextCache . set ( columns , byRow ) ;
261249 }
262250 let texts = byRow . get ( row ) ;
263- if ( texts === undefined ) {
264- texts = packQuickFilterTexts ( getQuickFilterTexts ( row , columns ) ) ;
251+ if ( ! texts ) {
252+ texts = getQuickFilterTexts ( row , columns ) ;
265253 byRow . set ( row , texts ) ;
266254 }
267255 return texts ;
268256}
269257
270- function textsInclude ( texts : QuickFilterTexts , term : string , termHasSeparator : boolean ) : boolean {
271- if ( typeof texts !== 'string' ) return texts . some ( text => text . includes ( term ) ) ;
272- if ( ! termHasSeparator ) return texts . includes ( term ) ;
273- // An empty joined string is no texts at all, not one empty text.
274- return texts !== '' && texts . split ( TEXT_SEPARATOR ) . some ( text => text . includes ( term ) ) ;
258+ function quickFilterTerms ( values : readonly string [ ] | undefined ) : string [ ] {
259+ return ( values ?? [ ] )
260+ . map ( value => String ( value ?? '' ) . trim ( ) . toLowerCase ( ) )
261+ . filter ( term => term !== '' ) ;
262+ }
263+
264+ function textsMatchTerms ( texts : readonly string [ ] , terms : readonly string [ ] ) : boolean {
265+ // Plain loops: this runs once per row on every keystroke.
266+ for ( let t = 0 ; t < terms . length ; t ++ ) {
267+ const term = terms [ t ] ;
268+ let found = false ;
269+ for ( let i = 0 ; i < texts . length ; i ++ ) {
270+ if ( texts [ i ] . includes ( term ) ) {
271+ found = true ;
272+ break ;
273+ }
274+ }
275+ if ( ! found ) return false ;
276+ }
277+ return true ;
275278}
276279
277280function compileQuickFilter ( values : readonly string [ ] | undefined , columns : GridColumnLookup | undefined ) : RowPredicate | null {
278- const terms = ( values ?? [ ] )
279- . map ( value => String ( value ?? '' ) . trim ( ) . toLowerCase ( ) )
280- . filter ( term => term !== '' ) ;
281+ const terms = quickFilterTerms ( values ) ;
281282 if ( terms . length === 0 ) return null ;
282- const separated = terms . map ( term => term . includes ( TEXT_SEPARATOR ) ) ;
283- return ( row ) => {
284- const texts = getCachedQuickFilterTexts ( row , columns ) ;
285- for ( let t = 0 ; t < terms . length ; t ++ ) {
286- if ( ! textsInclude ( texts , terms [ t ] , separated [ t ] ) ) return false ;
287- }
288- return true ;
289- } ;
283+ return ( row ) => textsMatchTerms ( getCachedQuickFilterTexts ( row , columns ) , terms ) ;
284+ }
285+
286+ // filterRows also keeps the texts of the last rows array it searched, by position, per column
287+ // lookup. Looking a text up by index is much cheaper than one WeakMap entry per row, and after an
288+ // edit (a new array with mostly the same row objects at the same positions) only the rows whose
289+ // object changed are read again.
290+ interface QuickFilterArrayTexts {
291+ rows : readonly GridRowModel [ ] ;
292+ texts : string [ ] [ ] ;
293+ }
294+ const quickFilterArrayCache = new WeakMap < GridColumnLookup , QuickFilterArrayTexts > ( ) ;
295+
296+ function getQuickFilterTextsByIndex ( rows : readonly GridRowModel [ ] , columns : GridColumnLookup ) : string [ ] [ ] {
297+ const previous = quickFilterArrayCache . get ( columns ) ;
298+ if ( previous && previous . rows === rows ) return previous . texts ;
299+ const texts = new Array < string [ ] > ( rows . length ) ;
300+ for ( let i = 0 ; i < rows . length ; i ++ ) {
301+ const row = rows [ i ] ;
302+ texts [ i ] = previous && i < previous . rows . length && previous . rows [ i ] === row
303+ ? previous . texts [ i ]
304+ : getQuickFilterTexts ( row , columns ) ;
305+ }
306+ quickFilterArrayCache . set ( columns , { rows, texts } ) ;
307+ return texts ;
290308}
291309
292310/**
@@ -356,6 +374,17 @@ export function filterRows<R extends GridRowModel>(
356374 filterModel : GridFilterModel ,
357375 columns ?: GridColumnLookup
358376) : R [ ] {
377+ const terms = columns && filterModel ? quickFilterTerms ( filterModel . quickFilterValues ) : [ ] ;
378+ if ( columns && terms . length > 0 ) {
379+ // The same result as createRowFilter's predicate: the quick filter and then the items.
380+ const itemsPredicate = compileGroup ( filterModel . items ?? [ ] , filterModel . logicOperator ?? 'and' , { columns, warned : new Set ( ) } ) ;
381+ const texts = getQuickFilterTextsByIndex ( rows , columns ) ;
382+ const result : R [ ] = [ ] ;
383+ for ( let i = 0 ; i < rows . length ; i ++ ) {
384+ if ( textsMatchTerms ( texts [ i ] , terms ) && ( ! itemsPredicate || itemsPredicate ( rows [ i ] ) ) ) result . push ( rows [ i ] ) ;
385+ }
386+ return result ;
387+ }
359388 const predicate = createRowFilter ( filterModel , columns ) ;
360389 if ( ! predicate ) return rows ;
361390 return rows . filter ( row => predicate ( row ) ) ;
0 commit comments