Rich text / foldkit-richtext
Performance
Transactions accumulate changes per block and copy each affected container once, so N edits in one paragraph run in O(N) instead of copying the array N times. Back-to-back benchmark means on one machine compare per-operation copying against accumulation:
| Workload | Before | After |
|---|---|---|
| Paste 50k characters into one run | 0.021 ms | 0.023 ms |
| Type one character into a 20k-character run | 0.024 ms | 0.025 ms |
| Toggle a mark over a 400-run selection | 2.07 ms | 1.80 ms |
| Toggle a mark over a 2000-run selection | 16.99 ms | 9.59 ms |
| Toggle a mark over 200 runs / 200 blocks | 1.47 ms | 1.48 ms |
| Delete a range spanning 100 paragraphs | 3.06 ms | 3.27 ms |
| Split a block inside a 400-run paragraph | 1.14 ms | 1.35 ms |
| Paste one paragraph into a 200-block document | 1.13 ms | 1.22 ms |
The large formatting case halves in time and scales linearly (5× the runs costs 5.3× the time instead of 8.2×). Differences under a few percent in the other rows reflect normal run-to-run noise.
apply caches validation per immutable block and builds each document's index once. Typing into a 4,000-paragraph document dropped from ~10 ms of whole-document decoding per edit to validating only the single replaced block, with identical schema enforcement.
The collaborative path types one character into the middle of an n-paragraph document (RichText.run, then Replicated.translate, applyOps, project, and resolve). Four optimizations—per-block validation, layered insert indexes in applyOps, entry reuse in project, and dirty-block shadow building in translate—cut latency across sizes (back-to-back means on one machine):
| Paragraphs | Before | After |
|---|---|---|
| 100 | 1.08 ms | 0.17 ms |
| 1,000 | 8.70 ms | 1.91 ms |
| 4,000 | 49.2 ms | 9.61 ms |
Remaining cost scales with document size because each new document builds its block, run, and node indexes, and project walks entries to reuse its run table. Structural deletes across B paragraphs batch contiguous joins into a single structural edit and single index rebuild (measured at 1.77 ms after batching vs. 3.06 ms before). Normalization still scans the dirty set including run IDs, leaving O(dirty) lookups in the merge pass.