Pretext.js is the official website for Pretext, a pure JavaScript/TypeScript text measurement and
layout engine created by Cheng Lou (chenglou), a former React core team member now at Midjourney.
The library solves a 30-year browser bottleneck: to know how tall a block of text will be,
developers have always had to render it in the DOM and ask the browser — a process that forces
expensive synchronous layout reflow.
Pretext eliminates this entirely with a two-phase architecture. The prepare() function measures
text once via Canvas and Intl.Segmenter, then the layout() function computes line breaks and
heights using pure arithmetic in ~0.0002ms — roughly 600× faster than traditional DOM measurement.
No reflow, no thrashing, no dependencies.
The website features an interactive playground where developers can experiment with Pretext in real
time, live examples demonstrating virtual scrolling, chat bubbles, multilingual content, and
accordion animations, and comprehensive documentation of the library's API. Pretext supports 12+
writing systems including CJK, Arabic, Hebrew, Thai, and mixed bidirectional text.
Pretext.js is MIT-licensed, weighs approximately 15KB, and works with any framework — React, Vue,
Svelte, or vanilla JavaScript. It is used in production at Midjourney to handle real-time AI text
streaming without layout shift.
6 ratings
AI video with native sound, paid per clip. Nothing stored.
A calmer Mac menu bar, one click away.
Enterprise Software. Intelligent Automation. Global Impact.
Comments
5Builder of hddhunt.com — compare HDD & SSD prices by cost per TB
Smart approach — avoiding DOM reflows for text measurement is a real pain point in dynamic layouts. Would be great for tooltip positioning and auto-resizing text areas.
AI baby name consultant — cultural, meaningful & personal
The fact that this came out of real production needs at Midjourney gives it a lot more credibility than most open-source libraries. The 600x speed improvement over DOM measurement is impressive, but what really sells it is the practical use cases on the site — virtual scrolling and chat bubbles are exactly the scenarios where layout thrashing becomes a visible performance problem. Supporting 12+ writing systems including bidirectional text out of the box is also huge, since most libraries treat that as an afterthought and you end up with broken layouts for RTL or CJK content.
Really impressed by the clean UX here. The onboarding flow is smooth and I like how you've focused on reducing friction at every step. Have you considered adding keyboard shortcuts for power users? That could make this even more sticky for daily workflows.
Founder of OrbitMeet — AI meeting co-pilot for multilingual professionals.
Avoiding DOM measurement is huge—layout thrash kills performance in canvas-heavy UIs. Benchmarks vs existing libraries and a tiny Next.js/React integration demo would be a perfect next step.
AI prompt manager for power users.
Avoiding DOM reflows for text measurement is a clever solution. The fact that it's only 15KB with zero dependencies makes it really easy to adopt. Nice work.