Get AI summaries of any video or article — Sign up free
He just crawled through hell to fix the browser… thumbnail

He just crawled through hell to fix the browser…

Fireship·
4 min read

Based on Fireship's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.

TL;DR

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?

Text height queries typically trigger layout reflow, where the browser recalculates geometry for elements to answer questions like paragraph height and line breaks. Reflow is described as one of the most expensive browser operations, and it becomes especially painful when an app needs many measurements during scrolling—such as with virtualized lists or masonry layouts.

How does pretext avoid the browser’s reflow cost?

pretext bypasses the standard text measurement pipeline. It uses the canvas API to get pixel widths of strings without involving the DOM or layout calculations. For height, it avoids DOM measurement too by using a custom algorithm that predicts line breaks and wrapping behavior rather than asking the browser to compute layout.

What makes text height harder than text width for this kind of library?

Width can be measured directly via canvas pixel metrics. Height depends on how text wraps into lines, which in turn depends on line-break rules that vary across browsers and across languages. The transcript frames this as near-impossible to get right manually, so the project relies on an iterative, test-and-compare approach to validate the algorithm against real browser behavior.

What is the basic pretext workflow described in the transcript?

First, text is prepared: it’s broken into segments and each segment’s width is cached. Then layout is called to compute total height and line count. The key claim is that this happens without touching the DOM or triggering reflow.

How does the transcript’s demo use pretext in a real UI pipeline?

A video-to-characters app uses pretext to compute a character grid: it prepares the transcript text into segments, then repeatedly calls layout per row to determine which characters land in which columns. It renders the video into an offscreen canvas sized to the grid, reads pixel brightness, and draws or skips each character cell based on whether the corresponding pixel is bright or dark.

Review Questions

  1. What specific browser operation makes text measurement expensive, and why does it become worse in virtualized or masonry layouts?
  2. How does pretext combine canvas-based width measurement with a separate line-break algorithm to compute height without DOM reflow?
  3. In the character-grid video demo, what roles do pretext and the offscreen canvas play, and how do they interact?

Key Points

  1. 1

    pretext targets fast, accurate text measurement in pure TypeScript while avoiding DOM-based layout reflows.

  2. 2

    Browser text height measurement commonly triggers layout reflow, which is costly and hampers text-heavy UI patterns.

  3. 3

    pretext measures string widths using the canvas API outside the DOM to avoid layout work.

  4. 4

    pretext computes text height via a custom line-break algorithm that accounts for browser and language wrapping differences.

  5. 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. 6

    A demonstrated application uses pretext to place characters in a grid, then uses an offscreen canvas to map video pixels to character brightness.

Highlights

pretext aims to compute text height and line count without triggering browser layout reflow—one of the biggest performance bottlenecks in dynamic text UIs.
Canvas API handles widths outside the DOM, while a dedicated line-break algorithm predicts wrapping to derive height.
The transcript’s video-to-characters demo shows pretext powering exact character placement, with brightness from an offscreen canvas shaping the final image.

Topics

  • Text Measurement
  • Browser Reflow
  • TypeScript Libraries
  • Canvas API
  • UI Layout

Mentioned