Edited by humans. Written by AI. How our editing works
All articles

Why Measuring Text Nearly Broke the Web—And How One Dev Fixed It

Cheng Lou built Pretext to bypass browser reflows—solving a 30-year performance problem developers didn't know they could fix. Here's what that means.

Rachel "Rach" Kovacs

Written by AI. Rachel "Rach" Kovacs

April 4, 20265 min read
Share:
Browser logos with "500x faster" text overlaid on dark background with a man in red lighting on the right side

Photo: Fireship / YouTube

There's a particular flavor of engineering problem that exists in plain sight for decades, causing daily frustration, yet somehow manages to avoid being treated as solvable. Text measurement in browsers is one of those problems.

Cheng Lou, former React core team member and current Midjourney engineer, just released Pretext—a TypeScript library that measures text without asking the browser to do it. This sounds technical and niche until you realize what it means: we've been building around a fundamental performance bottleneck since the browser was invented, and now we might not have to.

The Problem Nobody Wanted to Solve

Every time a browser needs to figure out how tall a paragraph is or where to break a line, it triggers a layout reflow. This operation recalculates the position and geometry of potentially every element on the page. It's computationally expensive—one of the most expensive things a browser does—and it happens constantly in any text-heavy interface.

Want to build a virtualized list that only renders visible messages? You need to know the height of each message before you can calculate which ones are on screen. Want a masonry layout with text blocks of varying sizes? Same problem. The traditional options are: render everything and measure it (slow), guess the dimensions (wrong), or architect around the limitation entirely (what most developers do).

"Ever since Al Gore invented the internet, rendering dynamic text has had a performance trade-off," the Fireship video explains. "This makes it unreasonably difficult to build any sort of text heavy UI like a virtualized list or a masonry layout."

This isn't a new problem. It's been present since the Clinton administration designed the text rendering pipeline. But treating it as unfixable became industry consensus.

Bypassing the Browser Entirely

Lou's solution is conceptually straightforward: don't ask the browser. Use the Canvas API to get pixel widths for text strings—Canvas lives outside the DOM, so no reflow. Then write a custom algorithm that accounts for how every major browser across every language handles line breaks, and calculate height yourself.

The conceptual simplicity masks brutal implementation complexity. Lou describes using AI models in what he calls "a recursive hellscape"—having them write line break logic, test it against real browsers, compare results, iterate for weeks. The goal was an algorithm that matched browser behavior without triggering browser calculation.

The result is an API that feels too simple for what it does:

// Prepare text (breaks into segments, caches widths)
const prepared = pretext.prepareWithSegments(text);

// Get dimensions without touching the DOM
const { height, lineCount } = layout(prepared);

No DOM access. No reflow. Just measurements.

What This Actually Enables

The Fireship demo shows text-based video rendering—drawing video frames using ASCII characters where brightness determines which letters appear. It's a technical flex, but the underlying capability is what matters: knowing exactly where every character lands before rendering anything.

That capability unlocks interfaces that were previously too expensive to build. Virtualized text-heavy UIs without the measurement overhead. Dynamic layouts that respond to content without recalculating everything. Text editors that can handle massive documents because they don't need to render offscreen content to know its dimensions.

The question isn't whether Pretext solves a real problem—it obviously does. The question is whether the browser's text measurement monopoly was actually necessary, or just something we accepted because challenging it seemed unreasonable.

The Security Angle Nobody's Discussing

From a security perspective, any library that reimplements core browser functionality deserves scrutiny. Browsers have spent decades hardening text rendering against Unicode exploits, bidirectional text attacks, and rendering inconsistencies that could be weaponized.

Lou's approach of using AI to match browser behavior across languages and systems is clever engineering, but it's also a potential attack surface. If Pretext's line break algorithm diverges from browser behavior in edge cases—especially with complex scripts or unusual Unicode sequences—you could end up with text that measures one way in Pretext but renders differently in the browser.

That divergence could matter for security-critical applications. Imagine a moderation system that uses Pretext to determine if a message fits within allowed bounds, but a browser renders it differently, exposing hidden content. Or a URL display that Pretext measures as safe but a browser interprets as malicious due to bidirectional text override characters.

To be clear: there's no evidence Pretext has these vulnerabilities. Lou's testing methodology specifically targeted browser matching. But introducing a parallel text measurement system means introducing parallel text measurement bugs. The browser's system has had three decades of exploitation attempts to refine it.

Developers adopting Pretext should understand they're trading a different set of risks—known browser performance costs for unknown algorithmic divergence risks. For many applications, that's a reasonable trade. For security-sensitive ones, it requires careful threat modeling.

What Success Looks Like

If Pretext succeeds, it won't be because every developer immediately rewrites their text handling code. It'll be because it proves the browser doesn't own foundational UI operations by divine right.

That proof-of-concept matters more than the specific implementation. Lou's library demonstrates that problems we've treated as unsolvable constraints might just be engineering challenges nobody funded. The text measurement problem existed in public for 30 years before someone with Lou's specific combination of skills, institutional knowledge, and apparent stubbornness decided to solve it.

The question now is: what other assumed constraints are just unquestioned technical debt?

Rachel 'Rach' Kovacs is Buzzrag's cybersecurity and privacy correspondent.

From the BuzzRAG Team

We Watch Tech YouTube So You Don't Have To

Get the week's best tech insights, summarized and delivered to your inbox. No fluff, no spam.

Weekly digestNo spamUnsubscribe anytime

More Like This

Orange app icon with radiating lines surrounded by gray folder tabs labeled Clients, Business, and YouTube, beside bold…

Browser Use CLI Gives AI Agents Web Control—For Free

New Browser Use CLI tool lets AI agents control browsers with plain English commands. Free, fast, and works with Claude Code—but raises questions about automation.

Dev Kapoor·4 months ago·6 min read
A URL comparison showing "http://localhost:3000/myapp" with a red X on the left and green checkmark on the right against a…

Vercel's Portless Tool: Weekend Project or Real Solution?

Vercel Labs released Portless to eliminate localhost port conflicts. Does this weekend project solve a real problem, or create new ones?

Rachel "Rach" Kovacs·5 months ago·5 min read
Man wearing headphones with surprised expression next to large green open source keyhole logo against code background

Cloudflare Just AI-Cloned Next.js and Open Source Is Shook

Cloudflare used AI to recreate Next.js in a week. The performance claims are wild, but the real story is what this means for open source's future.

Zara Chen·5 months ago·5 min read
Yellow heart-eyes emoji with closed smile against purple wireframe grid background, with "Modern CSS" text below

CSS Just Ate JavaScript's Lunch (And Nobody Noticed)

Modern CSS now handles carousels, tooltips, and UI interactions without JavaScript. Chrome's CSS Wrapped 2025 shows how far the language has come.

Mike Sullivan·4 months ago·6 min read
Text overlay reading "10 TECH TRENDS FOR 2026" with Google, OpenAI, and Meta logos, featuring a man's face with an…

Navigating 2026: Tech Trends and Your Role

Explore 2026's tech trends from AI to quantum, and learn how to engage confidently.

Rachel "Rach" Kovacs·6 months ago·3 min read
Eight illustrated icons representing dynamic programming concepts including algorithms, data structures, and optimization…

Dynamic Programming: From Theory to Practical Empowerment

Explore dynamic programming's practical power, transforming complex challenges into manageable solutions.

Rachel "Rach" Kovacs·6 months ago·4 min read
Developer at desk with dual monitors displaying analytics dashboards and data visualizations in a neon-lit tech environment

34 Self-Hosted Projects That Could Replace Your Cloud Stack

From AI email agents to thermal printer dashboards, these trending GitHub projects show what happens when developers get tired of subscription fees.

Rachel "Rach" Kovacs·3 months ago·6 min read
Bearded man wearing glasses and a beanie gestures toward camera with confused expression, text reads "NOW WHAT?

Why Your AI Agent Sits Idle After Installation

Installing an AI agent takes 10 minutes. Making it actually useful takes 40 hours. Here's why the industry keeps solving the wrong problem.

Rachel "Rach" Kovacs·3 months ago·6 min read

RAG·vector embedding

2026-04-15
1,294 tokens1536-dimmodel text-embedding-3-small

This article is indexed as a 1536-dimensional vector for semantic retrieval. Crawlers that parse structured data can use the embedded payload below.