Repository navigation
perf(*): normalize text once in TokenSearch and hole-sift MaxHeap - #868
Closed
maccarlton25 wants to merge 1 commit into
Closed
maccarlton25 wants to merge 1 commit into
maccarlton25 wants to merge 1 commit into
Conversation
## Summary Splits out the two standalone changes from krisk#866 onto `main`: 1. Single-pass field normalization in `TokenSearch` (`src/search/token/index.ts`) - Previously, a `K`-term token query ran `toLowerCase()` and `stripDiacritics()` `K` times per field (once inside each term's `FuzzySearch` instance). - `TokenSearch` now normalizes each term once at construction and each field once in `searchIn`, passing pre-normalized strings to `FuzzySearch` with `isCaseSensitive: true, ignoreDiacritics: false`. - Biggest impact is when `ignoreDiacritics: true`, where `stripDiacritics` runs `NFD` + combining-mark replacement + `NFC` once per field instead of `K` times. 2. Hole-sift `MaxHeap` (`src/tools/MaxHeap.ts`) - Replaces 3-write element swaps on every level of `_bubbleUp` and `_sinkDown` with single-write hole sifts, writing the moved item once at its final index. ## Benchmarks (`10,000` 3-field docs vs. `main`) Median of 9 interleaved rounds on Node.js v24: | Workload (10k docs) | `main` | This PR | Speedup | | --- | --- | --- | --- | | `TokenSearch` (2 terms) | `12.27 ms` | `10.98 ms` | `1.12x` | | `TokenSearch` (4 terms) | `21.27 ms` | `19.14 ms` | `1.11x` | | `TokenSearch` (6 terms) | `30.70 ms` | `26.94 ms` | `1.14x` | | `TokenSearch` + `ignoreDiacritics` (2 terms) | `36.39 ms` | `24.88 ms` | `1.46x` | | `TokenSearch` + `ignoreDiacritics` (4 terms) | `66.36 ms` | `33.39 ms` | `1.99x` | | `TokenSearch` + `ignoreDiacritics` (6 terms) | `93.89 ms` | `40.98 ms` | `2.29x` | ## Bundle Size | Artifact | `main` (raw / gzip) | This PR (raw / gzip) | Delta | | --- | --- | --- | --- | | `dist/fuse.min.mjs` | `32,616 / 11,469 B` | `32,722 / 11,514 B` | `+45 B gz` | | `dist/fuse.basic.min.mjs` | `22,545 / 8,513 B` | `22,528 / 8,516 B` | `+3 B gz` | TypeScript declaration outputs (`dist/*.d.ts`, `dist/*.d.cts`) have zero diff against `main`. ## Verification - `npm run lint` — 0 errors, 0 warnings - `npm run typecheck` — pass - `npm run build` — all CJS, ESM, minified, worker, and `.d.ts` targets built - `npm test` — 29/29 test files (`574/574` tests) pass - `npm run docs:build` — VitePress build clean
Contributor
Author
Owner
|
Landed in 3b9d9ea, thanks for splitting these out! I got identical output to main across 5,000 randomized searches, and the diacritics win is real: on accented text with |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Splits out the two standalone changes from #866 onto
main:Single-pass field normalization in
TokenSearch(src/search/token/index.ts)K-term token query rantoLowerCase()andstripDiacritics()Ktimes per field (once inside each term'sFuzzySearchinstance).TokenSearchnow normalizes each term once at construction and each fieldonce in
searchIn, passing pre-normalized strings toFuzzySearchwithisCaseSensitive: true, ignoreDiacritics: false.ignoreDiacritics: true, wherestripDiacriticsruns
NFD+ combining-mark replacement +NFConce per field instead ofKtimes.Hole-sift
MaxHeap(src/tools/MaxHeap.ts)_bubbleUpand_sinkDownwith single-write hole sifts, writing the moved item once atits final index.
Benchmarks (
10,0003-field docs vs.main)Median of 9 interleaved rounds on Node.js v24:
mainTokenSearch(2 terms)12.27 ms10.98 ms1.12xTokenSearch(4 terms)21.27 ms19.14 ms1.11xTokenSearch(6 terms)30.70 ms26.94 ms1.14xTokenSearch+ignoreDiacritics(2 terms)36.39 ms24.88 ms1.46xTokenSearch+ignoreDiacritics(4 terms)66.36 ms33.39 ms1.99xTokenSearch+ignoreDiacritics(6 terms)93.89 ms40.98 ms2.29xBundle Size
main(raw / gzip)dist/fuse.min.mjs32,616 / 11,469 B32,722 / 11,514 B+45 B gzdist/fuse.basic.min.mjs22,545 / 8,513 B22,528 / 8,516 B+3 B gzTypeScript declaration outputs (
dist/*.d.ts,dist/*.d.cts) have zero diffagainst
main.Verification
npm run lint— 0 errors, 0 warningsnpm run typecheck— passnpm run build— all CJS, ESM, minified, worker, and.d.tstargets builtnpm test— 29/29 test files (574/574tests) passnpm run docs:build— VitePress build clean