Performance claims in JavaScript are easy to make and hard to trust. "300x faster" sounds like marketing copy — so let's look at what's actually happening, why DOM text measurement is slow, and what the real numbers look like when you compare Pretext to the traditional approach.
When you use browser APIs to measure text — element.offsetWidth, getBoundingClientRect(), getComputedStyle() — you're triggering the browser's layout engine. This isn't a lightweight operation. The browser has to:
Step 4 is the killer: this is a layout reflow, and it invalidates the browser's cached layout tree. Do it 100 times in a loop, and you've triggered 100 full reflows — even if you're only measuring simple <span> elements.
Experienced developers know to batch DOM reads and writes to minimize reflows. But for dynamic text layout — where container sizes change on resize, font sizes respond to content, or text wraps around moving objects — batching is often impossible. You need the measurement before you can compute the next position.
This is the wall that text-heavy UIs hit. No amount of batching helps when the layout problem itself is inherently sequential.
Pretext bypasses the DOM layout engine entirely. Here's the actual pipeline:
prepare() phase (run once per text string):
<canvas> with ctx.measureText(), which uses the real font engine without triggering layoutlayout() phase (run every frame):
No DOM queries. No layout engine. No reflows. Just arithmetic over a flat array.
This is the scenario measured in the DOM vs Pretext benchmark demo built by community contributor clawsuo-web. It renders 500 independent text blocks and measures layout time on each resize event.
| Metric | DOM Approach | Pretext |
|---|---|---|
| Layout time (500 blocks) | 15–30ms | ~0.09ms |
| DOM operations | 500 reflows | 0 |
| Speedup | — | ~500x |
| Frame budget used (60fps = 16.67ms) | 90–180% | 0.5% |
At 500 blocks with the DOM approach, you're already over budget for a 60fps animation before you've drawn anything. Pretext leaves 99.7% of your frame budget for actual rendering.
The resize scenario is where the gap becomes most visible. When a user drags a resize handle, layout must recalculate on every mouse move event — potentially 60+ times per second.
With DOM measurement, resize handlers typically include debouncing (50–200ms delay) specifically to avoid layout thrash. With Pretext, you can layout on every mousemove event without any debouncing, because a layout() call for a 500-text batch costs ~0.09ms.
This is why demos like Drag Sprite Reflow — where a sprite drags through text and paragraphs reflow in real time — are possible with Pretext and practically impossible with DOM measurement.
For typographic animation (text as physics objects, particle systems, game tiles), layout must run every frame — at 60fps, that's once every 16.67ms.
DOM text measurement at 60fps is simply not viable for non-trivial amounts of text. Pretext's layout() runs in ~0.09ms per 500-text batch, meaning you can layout 300+ text blocks per frame and still have 15ms remaining for rendering.
This unlocks the entire category of text-based games and physics simulations in the pretext.cool showcase.
Pretext isn't the right tool for everything. DOM measurement is perfectly adequate when:
Pretext is the right tool when you need text layout to be fast enough to run on every frame — real-time reflow, animation, games, or data-dense UIs with live updates.
The DOM vs Pretext demo lets you see the performance difference in your own browser, on your own hardware. Run both approaches side-by-side and watch the frame rate counter.
Want to build something with Pretext? Browse the full community showcase for inspiration, or jump straight to the getting started guide.
Community benchmark demo by clawsuo-web. Pretext library by chenglou.