Calculate Text Height Without the DOM? Pretext Does It

A new open-source browser library called Pretext is turning heads in the web development world. Created by Cheng Lou, a former React core developer and the original mind behind the react-motion animation library, Pretext solves a surprisingly stubborn problem: calculating the height of line-wrapped text without ever touching the DOM. The project is generating serious buzz, as reported by Hacker News, where it pulled in 167 upvotes.

Why does this matter? The standard approach to measuring text dimensions in a browser requires rendering the text first, then reading its measurements back. That round-trip is slow. Really slow. For applications that need to calculate layouts for hundreds or thousands of text blocks (think virtualized lists, editors, or animation engines), this DOM dependency becomes a major bottleneck.

Pretext sidesteps the problem entirely with a two-phase architecture:

  • prepare() phase: Splits text into segments (words, but also soft hyphens, non-Latin characters, and emoji), measures them using an off-screen canvas, and caches the results. This step is heavier but only runs once.
  • layout() phase: Emulates the browser’s word-wrapping logic to calculate how many wrapped lines the text occupies at any given width, then returns the overall height. This runs fast and can be called many times with different widths.

That separation is the key insight. By front-loading the expensive measurement work, Pretext makes repeated layout calculations dramatically cheaper. This opens up new possibilities for text rendering effects in browser apps, including smooth animations that would have been impractical with traditional DOM measurement.

🌍 Multilingual and Battle-Tested

What stands out here is the testing rigor. Early tests rendered a full copy of The Great Gatsby across multiple browsers to verify measurement accuracy against a large volume of text. The test suite has since expanded to include lengthy public domain documents in Thai, Chinese, Korean, Japanese, Arabic, and more, all stored in a dedicated corpora/ folder.

“The engine’s tiny (few kbs), aware of browser quirks, supports all the languages you’ll need, including Korean mixed with RTL Arabic and platform-specific emojis,” Cheng Lou said.

🤖 Built with AI in the Loop

Cheng Lou noted that much of this accuracy was achieved by feeding browser ground truth data to Claude Code and Codex, then having them measure and iterate against real rendering at every significant container width. This process ran over weeks, a noteworthy example of using AI coding assistants not just for scaffolding, but for precision engineering against empirical data.

Why This Is Significant

Pretext is small (a few kilobytes), handles edge cases most libraries ignore (RTL text mixed with LTR, emoji width quirks across platforms), and requires zero DOM access for layout calculations. For developers building rich text editors, virtualized scrolling, or animation-heavy interfaces, this could eliminate one of the most persistent performance bottlenecks in browser rendering.

Cheng Lou’s track record adds weight here. React-motion changed how developers thought about animation in React. Pretext could do something similar for text layout.

For more details, check out the original discussion and demo links on Hacker News.

Scroll to Top