I thought I understood how text worked on the web. I did not. A library with more than 50,000 GitHub stars just showed me exactly how wrong I was, and I'm genuinely grateful for the education.
Here's the thing nobody really tells you when you start building for the browser: asking the DOM how tall a piece of text is costs more than you think. A lot more. And once you understand the mechanism, you can't unlearn it - every interface you've ever built suddenly looks slightly different.
The library is called pretext. Created by Cheng Lou - former React core team member, creator of ReasonML, now at Midjourney. It's about 15 kilobytes, has zero dependencies, and is MIT-licensed. It does exactly one thing: measures and lays out text without touching the DOM. Around 300 to 600 times faster than the approach we've all been using since roughly the Clinton administration.
The Browser's Dirty Little Secret
Every time you call getBoundingClientRect() or offsetHeight on a DOM element, the browser has to stop whatever it's doing and recalculate the layout of the entire page. Every element. Every position. The whole document. It then hands you back your number and quietly resumes. This is called a forced synchronous layout reflow - and it's the origin story of roughly 70% of the jank you've ever seen in a web interface.
For one element, imperceptible. For ten, probably fine. But if you have a virtual scroll list sizing 500 comments before rendering them, or a chat UI computing bubble heights as tokens stream in, or a design tool measuring text in a render loop - every single measurement triggers a full page recompute. The browser is basically Sisyphus: rolling the boulder up the hill, handing you a number, and rolling it back down for the next element. The comments in pretext's source say it plainly:
// Problem: DOM-based text measurement
// (getBoundingClientRect, offsetHeight)
// forces synchronous layout reflow.
//
// When components independently measure text,
// each measurement triggers a reflow
// of the entire document.
//
// This read/write interleaving can cost
// 30ms+ per frame for 500 text blocks.
30ms per frame. Six dropped frames at 60fps. The difference between an interface that feels instant and one that's thinking about it. And the truly maddening part: the browser knows exactly how to answer the question without doing all that work. It just won't - unless you ask it correctly. For thirty years, nobody gave us a clean library to do that.
It's the Heisenberg uncertainty principle of the browser: the act of measuring the DOM changes the DOM. Or rather, forces the browser to recalculate it. As physics metaphors for frontend development go - annoying but accurate.
Measure 200 text items. Left column forces a real DOM reflow on every call. Right uses canvas measurement and pure arithmetic - the pretext approach. Hit Run and watch.
Two Functions. That's the Entire API.
Pretext inverts the measurement model entirely. Instead of asking the DOM, it goes one level deeper - directly to the browser's font engine via canvas.measureText(). Canvas API calls bypass the layout tree. They query the font engine without triggering reflow. The browser was always capable of this. Nobody gave us a convenient, production-ready library around it. Until now.
The entire API is two functions. This is the Unix philosophy in library form - do one thing, do it perfectly, get out of the way:
import { prepare, layout } from '@chenglou/pretext' // prepare() - runs ONCE when text first appears. // Uses canvas.measureText(). Caches everything. // Returns an immutable handle you reuse forever. const handle = prepare( 'Your text here', '18px "DM Sans"' ) // layout() - runs on every resize. // Pure arithmetic over cached widths. // No DOM. No Canvas. No reflow. Ever. const { height, lineCount } = layout( handle, containerWidth, lineHeight )
The prepare() phase is the expensive one - about 17ms for a 500-text batch. It normalises whitespace, segments text using Intl.Segmenter, measures every word via Canvas, and caches the widths. Full CJK, Arabic, Hebrew, Thai, and mixed bidirectional text support - every human script. You pay this cost once, when text first appears. The handle it returns is immutable and reusable forever.
The layout() phase is the hot path: pure arithmetic over cached widths. No DOM reads. No Canvas calls. No string allocations. About 0.10ms for those same 500 texts. Call it on every resize, every frame, a thousand times a second if you want. It doesn't care. It's just arithmetic.
DOM: ~30ms per frame - six dropped frames at 60fps. Pretext: ~0.10ms total. This isn't an edge case. It's the everyday cost of building text-heavy UIs the old way.
Convenient. Slow. Everywhere.
- getBoundingClientRect forces full page reflow
- 30ms+ for 500 elements - six dropped frames
- Read/write interleaving makes it progressively worse
- Render target: DOM only
- Language support: whatever CSS decides today
- No pre-sizing before mount
Two functions. Your rules.
- canvas.measureText() - bypasses the layout tree
- ~0.10ms for 500 elements, pure arithmetic
- prepare() once, layout() forever
- DOM, Canvas, SVG, WebGL - all valid targets
- Full i18n: CJK, Arabic, Hebrew, Thai, bidi
- Pre-size anything before it's rendered
Who Built This - and Why That Matters
Cheng Lou isn't a newcomer who woke up and decided to rewrite the web. He was a core contributor to the React team, created ReasonML (typed OCaml for the JavaScript ecosystem - the kind of project you only attempt if you have very strong opinions about type systems and the patience of a Jedi Master on a slow day), and is now building at Midjourney. The text-layout problem is one he's been working on for over a decade. The archived chenglou/text-layout repository - now pointing to pretext as its production evolution - shows the paper trail. Sebastian Markbage planted the original architectural seed. Cheng Lou spent years making it production-ready.
This matters because there's a specific category of open-source library that only exists because someone genuinely exceptional decided to make it. Not because there was a business model. Not because a company needed it internally and threw it over the wall. But because the problem was real, the existing approaches were wrong at a fundamental level, and they had the exact combination of skills, stubbornness, and available weekends to fix it properly. SQLite is that library for embedded databases. React was that library for component UIs. Pretext is shaping up to be that library for text layout.
The browser always knew how to answer the question without doing all that work. Pretext is the first library to figure out how to ask it correctly.
What It Actually Unlocks
This is the part that moved the creative part of my brain. Because once text measurement isn't a DOM operation, the render targets change completely.
Canvas text that knows its own dimensions. Properly wrapped, accurately measured, without a hidden div anywhere in sight. If you've ever tried to build a canvas-based layout and had to use a hidden DOM node just to figure out how tall your paragraph would be, you know the exact grimace this causes. Pretext removes that entirely.
WebGL and SVG rendering. The measured line data that drives DOM layout can drive GPU-rendered text or SVG paths equally well. The library's prepareWithSegments() path gives you the per-line cursor data needed to position text in arbitrary coordinate spaces. The same logic, different surface.
Magazine-style column flows around images. The layoutNextLine() iterator API lets you specify a different available width for each individual line - narrower beside the floated image, full-width below it. CSS float behaviour, but in your own render loop, at 60fps, in any rendering context.
AI chat UIs that pre-size message bubbles before tokens arrive - no ResizeObserver hacks, no layout jank as content streams in, just accurate dimensions from arithmetic. Given how much of the current web is a chat interface, this is genuinely not a niche use case.
Games, physics, generative work. The community demos at pretext.cool include a Tetris clone built entirely from text characters running at 60fps, a music visualiser where every bar label updates per frame, and text physics experiments where paragraphs behave like rigid bodies. Not party tricks. That's what happens when the question "how tall is this text?" stops costing anything. Suddenly text is the TRON Grid - but made of actual type.
Why I Care About This Specifically
When I built the TTS buttons, the language switcher and the bilingual hero sections for this blog, I ran into text layout questions I mostly chose not to examine too closely. I had a gentleman's agreement with the browser: it handles measurement, I handle aesthetics, we don't discuss what happens in between.
Pretext changes the category of what I think about building - not as an upgrade to my current setup, but as an expansion of what's in the practical toolkit. A canvas-based typographic layout that responds to article content. A WebGL reading experience. Text physics for a portfolio interaction. Things previously filed under "too much DOM gymnastics to be worth it" now sit in a completely different column: the one labelled actually possible on a Tuesday afternoon.
For a creative designer who codes - as opposed to a developer who designs - this is exactly the kind of library that matters. Not because it makes familiar things faster. Because it makes things possible that weren't in the practical toolkit before. I think of it like the moment Han Solo finally got the Millennium Falcon's hyperdrive actually working. The destinations didn't change. The journey became a completely different thing.
Five Things to Do With This Information
chenglou.me/pretext and pretext.cool. Five minutes. Let the demos show you what the thing is before the API tells you what it does. The Tetris clone alone will reframe your understanding of what the browser can actually do.
npm install @chenglou/pretext. Two imports. One rule: fonts must be loaded before calling prepare(). That's the only real gotcha. The README is genuinely well-written and gets you to a working example faster than most docs I've encountered.
The hidden divs. The getBoundingClientRect calls in a useEffect. The "I added 32px padding and hoped for the best" solutions. You know where they live. Now you know what to replace them with - and more importantly, why they were wrong in the first place.
What would you build if text measurement was free and Canvas, SVG, and WebGL were all equally valid surfaces? This is a genuine creative prompt - not rhetorical. Write down the answer before the Tuesday afternoon when you actually have time to try it.
The archived chenglou/text-layout shows where pretext came from - over a decade of thinking, made visible. Worth an hour for anyone who cares about how good infrastructure gets built by people who care enough to keep going.
The 15KB Philosophy
Pretext is about 15 kilobytes gzipped. Zero dependencies. MIT licence. It doesn't want to be a framework. It doesn't want to replace CSS. It doesn't have a roadmap with seventeen items labelled "coming soon" or a Discord server with its own economy. It solves one problem - text measurement - completely, correctly, and for free.
There's a word for this in engineering: orthogonality. A component is orthogonal when it does one thing and doesn't secretly do five others behind the scenes. When changing it doesn't cause unexpected effects somewhere else. Pretext is orthogonal. You can adopt it for exactly one part of one component, and nothing else in your stack changes. That's rarer than it sounds.
In a world where a fresh project scaffold starts at 300MB of node_modules before you've written a single line of actual product code, there is something quietly radical about a library that fits in 15KB, handles every human script on the planet, runs in Web Workers, works server-side via Canvas polyfill, and needs no configuration beyond a font string. It's not minimalism for its own sake. It's the result of understanding a problem well enough to know exactly what belongs and exactly what doesn't. That kind of work is rare. When it shows up - more than 50,000 stars in - it's worth your attention.
Personal perspective. Performance figures from the pretext README and official benchmarks. Browser behaviour may vary. Not sponsored - I just think this library is genuinely good.
- chenglou/pretext - GitHub (51k stars in Nov 2026, MIT): github.com/chenglou/pretext
- Official demos - Cheng Lou: chenglou.me/pretext
- Community demos & playground: pretext.cool
- NPM package (@chenglou/pretext): npmjs.com/package/@chenglou/pretext
- Archived predecessor - design history: github.com/chenglou/text-layout
Have a project at the intersection of design, technology, and creative coding? I work with companies and people who want honest, human communication - and who'd rather talk it through than sit through a discovery call. Let's see if there's something here.