Performance: more performant filtering diacritic normalization (#13347)

This commit is contained in:
shamoon
2026-07-27 22:13:16 +00:00
committed by GitHub
parent 98b66fdf24
commit 7eee8538ec
2 changed files with 25 additions and 2 deletions
+16
View File
@@ -14,4 +14,20 @@ describe('text search utilities', () => {
expect(matchesSearchText('Tax\u00e9s 2026', 'taxe 26')).toBeTruthy()
expect(matchesSearchText('taxes 2026', 'tax receipt')).toBeFalsy()
})
it('matches a large set of tag names without blocking input', () => {
const tagNames = Array.from(
{ length: 1280 },
(_, index) =>
`Customer Party ${index} Jos\u00e9 M\u00fcller \u00c5ngstr\u00f6m`
)
const start = performance.now()
const matches = tagNames.filter((name) => matchesSearchText(name, 'party'))
const duration = performance.now() - start
expect(matches).toHaveLength(tagNames.length)
// The previous implementation took roughly 500 ms for this workload.
expect(duration).toBeLessThan(250)
})
})
+9 -2
View File
@@ -1,10 +1,17 @@
import { normalizeSync } from 'normalize-diacritics'
import { diacritics } from 'normalize-diacritics/diacritics'
export type SearchTextValue =
string | number | boolean | bigint | null | undefined
export function normalizeSearchText(value: SearchTextValue): string {
return normalizeSync(String(value ?? '')).toLocaleLowerCase()
const normalized = diacritics.reduce(
(text, replacement) => {
return text.replace(replacement.diacritics, replacement.letter)
},
String(value ?? '')
)
return normalized.toLocaleLowerCase()
}
export function matchesSearchText(