Created
Mar 30, 2026
Last Modified
1 week ago

What is Pretext JS? How It Fixes Browser Reflow and Makes Text 500x Faster

What is Pretext JS? How It Fixes Browser Reflow and Makes Text 500x Faster

Pretext interactive ASCII dragon Demo

CSS isn’t dead… but one of its biggest limitations just got exposed.

For the last 30 years, browsers have handled text layout using the DOM.
Every time text changes, the browser recalculates layout — a costly process called reflow.

That’s why even today, apps still feel:

  • laggy

  • janky

  • slow under heavy updates

Now, a new library called Pretext JS, created by Cheng Lou, is changing that completely.

👉 It removes the DOM from text layout.
👉 It replaces layout with pure JavaScript calculations.
👉 And in heavy scenarios, it can be hundreds of times faster.

This isn’t just a performance trick…
It’s a completely new way to think about text rendering on the web.


What is Pretext JS?

Pretext JS is a high-performance JavaScript/TypeScript library for multiline text measurement and layout.

Unlike traditional approaches, it:

  • does NOT use the DOM for measurement

  • uses the browser’s font engine via Canvas API

  • calculates layout using pure math

👉 In simple words:

Pretext lets you measure and layout text without triggering browser reflow.

It was open-sourced by Cheng Lou (ex-React core contributor) and quickly gained popularity because it solves a long-standing frontend bottleneck.


The Real Problem: Browser Reflow

Whenever you change or measure text in the DOM:

javascript
const height = element.offsetHeight;

The browser must:

  1. Recalculate styles

  2. Layout (Reflow) ❌ expensive

  3. Paint

  4. Composite

👉 Even reading layout values triggers reflow.

And if this happens repeatedly (chat apps, animations, lists), you get:

  • layout thrashing

  • frame drops

  • poor UX

This problem has existed for decades because DOM layout is inherently expensive.


How Pretext Fixes This

Pretext takes a completely different approach:

👉 Instead of asking the DOM for layout…

👉 It calculates layout itself.

Core Idea:

  • Use canvas.measureText() (same font engine as browser)

  • Cache all word widths

  • Simulate line wrapping in JavaScript

Result:

  • ❌ No DOM reads

  • ❌ No reflows

  • ✅ Pure arithmetic layout


How Pretext Works (Simple Explanation)

Pretext follows a 2-step architecture:

1️⃣ prepare() → One-time setup

  • splits text into segments

  • measures each word using Canvas

  • caches widths

This step is slightly expensive (~19ms for large batches).


2️⃣ layout() → Fast path

  • uses cached widths

  • calculates line breaks

  • returns height & layout

👉 This is extremely fast (~0.09ms for large batches).


💡 That’s where the “500x faster” claim comes from —

because repeated layout becomes just math.


How to Use Pretext (Implementation)

Install

bash
npm install @chenglou/pretext

Basic Example

js
import { prepare, layout } from '@chenglou/pretext'

// Step 1: prepare text
const prepared = prepare('Hello world 🚀', '16px Inter')

// Step 2: calculate layout
const { height, lineCount } = layout(prepared, 300, 24)

console.log(height, lineCount)

👉 No DOM. No reflow. Just calculation.


Important Tip

Font must match your CSS exactly:

javascript
'16px Inter'

Otherwise measurement will be incorrect.


Real Use Cases

Pretext is not for everything — but where it fits, it’s insane.

🔥 Best Use Cases:

  • Chat apps (dynamic messages)

  • Virtualized lists

  • Masonry layouts

  • Typography animations

  • AI-generated streaming content

  • Canvas/WebGL text rendering

👉 Example:

You can calculate chat message height before rendering — no layout shift.


Pretext vs Traditional DOM

Feature

DOM

Pretext

Reflow

Yes ❌

No ✅

Performance

Slow under load

Very fast

Measurement

DOM APIs

Canvas + math

Animations

Janky

Smooth


Is Pretext Replacing CSS?

Short answer: No.

CSS still handles:

  • layout (Flexbox, Grid)

  • styling

  • responsiveness

👉 Pretext only replaces text measurement logic.

Think of it like:

WebGL didn’t replace HTML… it unlocked new possibilities.


Why Pretext Matters

According to Cheng Lou, text layout has been one of the biggest bottlenecks in UI engineering.

By solving it, Pretext enables:

  • smoother interfaces

  • real-time text effects

  • new UI patterns

👉 This could redefine how we build:

  • editors

  • dashboards

  • AI interfaces


⚠️ Limitations

Pretext is still evolving, so:

  • No full SSR support yet

  • Needs exact font config

  • Not a replacement for all layouts


Official Resources


Final Thoughts

For years, developers accepted reflow as “just how the web works.”

Pretext challenges that assumption.

Instead of optimizing the DOM…

👉 it avoids the DOM entirely.

And that simple idea unlocks something powerful:

Faster, smoother, more dynamic web experiences.


FAQs

What is Pretext JS?

A JavaScript library for text layout without DOM reflow.

Is Pretext faster than DOM?

Yes, especially in repeated layout scenarios.

Can Pretext replace CSS?

No, it complements CSS.

Is Pretext production ready?

Early-stage but promising.