# Performance

> Measured open, layout, typing and memory figures for a 300-page document in Birch UI Document Editor, how incremental layout and virtualization work, and how to tune them.

## Measured

The figures below come from `e2e/tests/document-perf.spec.ts`, which builds an annual report of 3,515 blocks (headings, justified paragraphs, lists, a table in every chapter and a picture in every second one) and drives it in Chrome through Playwright on a developer laptop. The typing rows are the median of five runs made alone, with one worker, while the load average of the machine was below 1.5; the range of the five runs is given for the row that decides whether a keystroke fits in a frame. The other rows are single runs. The document is the one that used to be 300 pages; with line heights taken from the font it is now 350.

| Measure | No footnotes | 396 footnotes | 1,287 footnotes |
| --- | --- | --- | --- |
| Pages | 350 | 365 | 396 |
| First page painted and editable | 368 ms | 299 ms | 313 ms |
| Layout complete (exact page count) | 1.51 s | 2.17 s | 2.72 s |
| Main-thread time per keystroke, mean | 13.0 ms | 10.1 ms | 10.4 ms |
| Keystroke handling, 95th percentile | 3.9 ms | 3.8 ms | 3.9 ms |
| Layout and paint of the edited frame, 95th percentile | 13.0 ms | 10.0 ms | 10.8 ms |
| Handling plus layout and paint, 95th percentile | 16.9 ms (14.3 to 17.8) | 13.7 ms (13.6 to 16.0) | 14.7 ms (14.0 to 17.5) |
| Keystroke to the end of the next frame, median | 20 ms | 20 ms | 21 ms |
| Keystroke to the end of the next frame, 95th percentile | 28 ms | 26 ms | 27 ms |
| Scrolling | 61 frames/s | 60 frames/s | 57 frames/s |
| JavaScript heap after opening | 62 MB | 52 MB | 52 MB |

Keystrokes are typed in the middle of the document, the worst case for repagination. "Main-thread time" is the task time Chrome reports through the DevTools protocol for 100 keystrokes, minus the idle baseline, divided by 100. "Handling" is the time spent in the key and input event handlers; "layout and paint" is the frame that shows the edit. The work for one keystroke fits in one 16.7 ms frame in the two documents with footnotes and is at the edge of it without: the median 95th percentile there is 16.9 ms, and two of the five runs were above 17 ms. When other suites run on the machine at the same time the same figure rises to 75 ms and more, so measure on a quiet machine. "Keystroke to the end of the next frame" also contains the wait for the next frame, up to 16.7 ms, which is why it is larger. The test fails when the mean exceeds 20 ms, handling exceeds 16 ms or handling plus layout and paint exceeds two frames. Building the print document for a 37-page document took 10 ms.

### A real Word file

`e2e/tests/document-docx-open.spec.ts` opens `08-long.docx` from the independent review, a 160 kB file with 1,230 paragraphs, 60 tables and 110,863 words, through `importDocx` in Chrome:

| Measure | Value |
| --- | --- |
| Pages | 204 (LibreOffice: 203; the file ends with a page break, and the editor puts the last paragraph mark on a new page as Word does) |
| `importDocx` resolved (file read, document shown) | 210 to 290 ms |
| First frame painted | 250 to 360 ms |
| Layout complete (exact page count) | 0.8 to 1.5 s |
| `insertText` near the start of the document, median of 30 | 3.5 to 6.8 ms (90th percentile 6.5 to 10.4 ms) |

The ranges are three single runs on a developer laptop that other builds were using at the time: the slowest run was taken at a load average above 40, the two fastest with the spec running alone at a load average of 5 to 7. They show the order of magnitude, not a benchmark; measure on your own hardware. The test fails above 6 s to the first frame, 60 s to complete layout and 50 ms median per insertion.

**What still scales with the document.** The content of far pages stays in the DOM (6,174 elements for this file); only its layout and paint are skipped with `content-visibility`. Repagination after an edit resumes from the edited page only for a document with one section; a document with section breaks is repaginated from its first page after every edit (measured blocks are still cached, so this is arithmetic over the block heights, not a re-measure). Numbering labels of an imported Word list are recomputed for the whole document on each change (one pass over the blocks; the decorations are rebuilt only when a label or its position changes). The word count reported by `contentChange` recounts only the block that changed, and the steps of the `transaction` event are serialised only when a listener reads them.

## How it stays fast

- **Incremental measurement.** The editor keeps the measured height of every block. After an edit only the blocks that changed are measured again, and pagination resumes from the page before the change and stops as soon as the page breaks line up with the previous result.
- **Background layout.** A long document is measured in slices between frames. The first screen is laid out at once, so the document is editable while the rest is still being measured; `getPageCount()` is an estimate until the layout is complete.
- **Virtualization.** Blocks away from the viewport are skipped by the browser with `content-visibility`, and page frames, headers, footers and footnote areas exist only near the viewport. The document stays one editable flow, so selection, find and undo work across the whole of it.

```ts
await editor.whenLayoutReady(); // resolves when the page count is exact
editor.isLayoutComplete();
editor.on('layoutComplete', ({ pageCount, durationMs }) => {});
editor.getLayoutStats(); // { pages, blocks, measured, cacheHits, lastLayoutMs, passes, complete, virtualized, reuse }
```

Wait for `whenLayoutReady()` before you read page numbers, call `getPageInfo`, or call `getPrintHtml()` or `print()` from code right after loading a long document. `exportPdf()` waits for the layout itself. `isLayoutComplete()` is `false` from the moment an edit is made until the pass that follows it has run.

## Tuning

- `virtualize: 'auto'` (the default) turns the incremental path on for documents with 240 top-level blocks or more. `true` forces it and `false` lays out every block eagerly, which is simpler to reason about for short documents that you post-process.
- Pictures with a `width` and `height` do not move the layout when they load. Pictures without them cause a second measurement.
- Web fonts that arrive after the first layout change line breaks. Load the fonts your templates use before creating the editor, or call `relayout()` when `document.fonts.ready` resolves.
- A list or a table is one block however long it is: the cache works per top-level block, so an edit inside it measures the whole list or table again.
- Work that runs on every keystroke is kept per top-level block: footnote numbering, tab stops, table and caption decorations, form detection and change bars reuse what they found for the blocks that did not change.
- `autosave.save` receives the full `getJSON()` result; choose `autosave.delay` with the size of your documents in mind.
