He just crawled through hell to fix the browser…

By Fireship

Share:

Key Concepts

  • Pretext: A high-performance, pure TypeScript library for text measurement.
  • Layout Reflow: A costly browser process where the engine calculates the geometry and position of elements; triggered by DOM-based text measurement.
  • Canvas API: A web technology used to render graphics and measure text dimensions outside of the standard DOM flow.
  • Virtualization: A technique for rendering large lists by only processing elements currently visible to the user.
  • Text Measurement Pipeline: The traditional, expensive method of querying the browser for text height/width.

1. The Problem: Browser Performance and Text Rendering

Rendering dynamic text in web applications has historically been hindered by the "layout reflow" tax. Whenever a developer queries the browser for the height or width of a text element, the browser must perform a reflow to calculate the geometry of the entire page. This makes building text-heavy interfaces—such as virtualized lists or masonry layouts—computationally expensive and prone to performance bottlenecks.

2. The Solution: Pretext Engineering

Developed by Changloo (former React core team member and Midjourney engineer), Pretext bypasses the traditional DOM-based text measurement pipeline.

  • Width Calculation: Instead of querying the DOM, Pretext utilizes the Canvas API. Because the canvas exists outside the DOM, it can calculate the pixel width of any string in any font without triggering a reflow.
  • Height Calculation: To solve for height, Changloo developed a custom algorithm that accounts for cross-browser and cross-language line-break rules.
  • Methodology: The algorithm was refined through a recursive testing process where AI agents ("clankers") generated line-break logic, tested it against actual browser behavior, and iterated until the results were consistent and accurate.

3. Step-by-Step Process: Using Pretext

Pretext provides a simplified API to handle complex text layouts:

  1. prepare: The library breaks the text into segments and caches the pixel width of each segment.
  2. layout: The developer calls this function to retrieve the total height and line count of the text.
  3. Execution: These steps occur entirely outside the DOM, allowing for high-performance UI updates without triggering expensive browser layout calculations.

4. Real-World Application: ASCII Video Rendering

The video demonstrates a creative application of Pretext: rendering a video using text characters based on pixel brightness.

  • Grid Mapping: The app uses prepare to cache segment widths and layout in a loop to determine character placement per column.
  • Brightness Calculation: The video is drawn to an offscreen canvas. The system reads raw pixel data and calculates brightness.
  • Sculpting: If a video pixel is bright, the corresponding character cell is rendered; if dark, it is skipped. Pretext provides the precise character positioning required to make this visual effect possible.

5. Key Arguments and Perspectives

  • Foundational Shift: The creator argues that Pretext is a "foundational piece of UI engineering" because it proves that developers do not have to rely on the browser’s native, slow text-measurement pipeline.
  • Performance vs. Complexity: By offloading measurement to the Canvas API and custom algorithms, developers can build complex, data-heavy UIs (like a 10,000-message chat app) that remain performant and responsive.

6. Notable Quotes

  • "What Chang figured out is that you don't actually need to ask the browser for text dimensions at all."
  • "Pretext handles the hard part of knowing exactly where every character belongs, and the canvas handles the rest."

7. Synthesis and Conclusion

Pretext represents a significant advancement in front-end infrastructure by decoupling text measurement from the DOM. By leveraging the Canvas API and a robust, AI-tested algorithm for line-break logic, it eliminates the performance penalties associated with layout reflows. While its long-term impact remains to be seen, it serves as a successful proof-of-concept that developers can reclaim control over text rendering, enabling more complex and performant UI patterns in modern web applications.

Chat with this Video

AI-Powered

Hi! I can answer questions about this video "He just crawled through hell to fix the browser…". What would you like to know?

Chat is based on the transcript of this video and may not be 100% accurate.

Related Videos

Ready to summarize another video?

Summarize YouTube Video