Pretext.js is a 15KB open-source TypeScript library by Cheng Lou (Midjourney engineer, former React core team member) that measures and lays out text without touching the DOM, bypassing expensive browser layout reflows. It uses a two-phase approach: a prepare() phase leveraging the Canvas API's measureText() to cache per-segment pixel widths, and a layout() phase using pure arithmetic to compute line counts and heights. This enables advanced UX patterns like infinite/virtual lists, masonry layouts, and scroll anchoring to run at 60–120 FPS. Benchmarks show a single layout operation on 500 text blocks takes ~0.09ms, up to 600x faster than DOM-based methods. The library supports multilingual text including RTL and mixed scripts, achieved by using Claude and Codex in an AI loop to reverse-engineer browser layout calculations. The repo gained 16,000 GitHub stars within 24 hours of release.
Sort: