An engineer just tackled a UI problem we’ve been struggling with for 30+ years. Text layout. Every time a web app needs to know: “How big is this text?” It asks the browser. And the browser: • pauses execution • recalculates layout (reflow) • then returns the measurement That means it’s not just measuring text it’s recalculating the entire page. This is one of the biggest reasons behind janky scrolling and unstable UI. ⸻ Now here’s the interesting part. Pretext.js takes a completely different approach. How it works Instead of asking the DOM, it computes layout independently. 1. Uses Canvas (measureText) • Gets accurate text metrics without triggering layout 2. Splits text into segments • Handles proper word boundaries 3. Measures once and caches 4. Computes layout using pure math • line breaks • wrapping • total height No DOM reads. No reflow. No blocking. ⸻ Why this matters • Predict layout before rendering • Eliminate layout thrashing • Smooth chat apps, feeds, and virtualized lists • Better performance at scale ⸻ Reality check • Not full CSS parity yet • Requires proper font handling • Minor differences vs actual DOM rendering But the direction is important. ⸻ Takeaway We’ve been asking the browser for layout. This shows we can compute it ourselves. —— GitHub https://lnkd.in/dx7eQnUw Demo credits • Work by Cheng Lou #javascript #frontend #webdevelopment #reactjs #performance #softwareengineering #webperf #uilayout

To view or add a comment, sign in

Explore content categories