Skip to content

perf(*): normalize text once in TokenSearch and hole-sift MaxHeap - #868

Closed
maccarlton25 wants to merge 1 commit into
krisk:mainfrom
maccarlton25:perf/token-search-and-maxheap
Closed

maccarlton25 wants to merge 1 commit into
krisk:mainfrom
maccarlton25:perf/token-search-and-maxheap

Conversation

@maccarlton25

Copy link
Copy Markdown
Contributor

Summary

Splits out the two standalone changes from #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

## 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
@maccarlton25

Copy link
Copy Markdown
Contributor Author

@krisk thanks for the feedback on #866, glad you were already looking the same direction!

@krisk krisk closed this in 3b9d9ea Nov 23, 2026
@krisk

krisk commented Nov 23, 2026

Copy link
Copy Markdown
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 ignoreDiacritics, token search went from 1.6x faster at 2 words to 3x at 6 words. It'll be in the next 7.6 beta.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants