Show HN: uWrap.js – A faster and more accurate text wrapping util in < 2KB
Published on: 2025-05-11 22:03:04
⏎ μWrap
A 10x faster and more accurate text wrapping util in < 2KB (min) (MIT Licensed)
Introduction
uWrap exists to efficiently predict varying row heights for list and grid virtualization, a technique for UI performance optimization when rendering large, scrollable datasets. Doing this both quickly and accurately turns out to be a non-trivial task since Canvas2D provides no API for text wrapping, and measureText() is quite expensive; measuring via DOM is also a non-starter due to poor performance. Additionally, font size, variable-width fonts, letter-spacing , explicit line breaks, and different white-space choices affect the number of wrapped lines.
Notes:
Today, works most accurately with Latin charsets
Does not yet handle Windows-style \r
explicit line breaks
explicit line breaks Only pre-line wrapping strategy is implemented so far
Performance
uWrap handily out-performs canvas-hypertxt in both CPU and memory usage by a wide margin while being significantly more accurate
... Read full article.