He just crawled through hell to fix the browser…
Based on Fireship's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.
pretext targets fast, accurate text measurement in pure TypeScript while avoiding DOM-based layout reflows.
Briefing
A former React core team engineer at Midjourney, Changlu, is betting that browser text measurement no longer has to be expensive. His library, pretext, aims to deliver fast, secure, accurate, and comprehensive text metrics in pure TypeScript—without forcing the browser to run layout reflows just to figure out line breaks, paragraph height, or where text should wrap. That matters because text-heavy UI patterns like virtualized lists and masonry layouts often stall on the cost of repeatedly asking the browser for dimensions.
In most browsers, measuring text height triggers layout reflow: the engine recalculates geometry for elements across the page to answer questions like “how tall is this paragraph?” Reflow is among the most expensive operations a browser can perform, and it becomes a bottleneck when an app needs thousands of measurements during scrolling or layout. pretext’s core move is to bypass the browser’s standard text measurement pipeline. For widths, it leans on the canvas API, which can measure string pixel widths outside the DOM without layout work. The harder part is height, because height depends on line breaking rules—rules that vary across browsers and languages.
To solve line breaks, Changlu built a custom algorithm that predicts how text wraps, then used automated “clankers” (AI workers) to generate and validate the logic. The workflow described is iterative: produce line-break logic, test it against real text in real browsers, compare results, and repeat until the algorithm is stable. The end product is a simple API: first “prepare” text by segmenting it and caching each segment’s width, then call “layout” to compute total height and line count without touching the DOM or triggering reflow.
The transcript also shows why that capability unlocks new UI techniques. One demo builds a character grid from video frames: the app uses pretext to determine exactly which characters belong in which columns and rows, then renders the video into a tiny offscreen canvas sized to the grid. It reads pixel brightness from the canvas and uses that brightness to decide whether each character cell is drawn or skipped—effectively sculpting an image out of letters. In that pipeline, pretext handles the precision of character placement, while canvas handles the pixel-level rendering.
Even if pretext doesn’t become the default foundation for all UI measurement, it’s presented as proof that browsers don’t have to “own” text measurement. The broader implication is practical: if apps can compute text geometry cheaply and deterministically, developers can build smoother, more scalable text-heavy interfaces without paying the reflow tax.
Cornell Notes
pretext is a TypeScript text measurement library designed to compute text height, line count, and wrapping without triggering browser layout reflows. It measures string widths using the canvas API (outside the DOM), then uses a custom line-break algorithm to predict how different browsers and languages wrap text. The library’s workflow is to prepare text by segmenting and caching widths, then call layout to get total height and line count. That shift matters because reflow is expensive and makes virtualized lists and masonry layouts difficult to scale. The transcript also demonstrates a character-grid video renderer where pretext determines exact character placement while an offscreen canvas provides pixel brightness for the final image.
Why does measuring text height often become a performance problem in browsers?
How does pretext avoid the browser’s reflow cost?
What makes text height harder than text width for this kind of library?
What is the basic pretext workflow described in the transcript?
How does the transcript’s demo use pretext in a real UI pipeline?
Review Questions
- What specific browser operation makes text measurement expensive, and why does it become worse in virtualized or masonry layouts?
- How does pretext combine canvas-based width measurement with a separate line-break algorithm to compute height without DOM reflow?
- In the character-grid video demo, what roles do pretext and the offscreen canvas play, and how do they interact?
Key Points
- 1
pretext targets fast, accurate text measurement in pure TypeScript while avoiding DOM-based layout reflows.
- 2
Browser text height measurement commonly triggers layout reflow, which is costly and hampers text-heavy UI patterns.
- 3
pretext measures string widths using the canvas API outside the DOM to avoid layout work.
- 4
pretext computes text height via a custom line-break algorithm that accounts for browser and language wrapping differences.
- 5
The library’s API workflow is to prepare text by segmenting and caching widths, then call layout to get total height and line count.
- 6
A demonstrated application uses pretext to place characters in a grid, then uses an offscreen canvas to map video pixels to character brightness.