Different truncation scenarios require different trade-offs: plain Latin text can be measured quickly in memory with Canvas, CJK text needs browser line-breaking rules, rich text must preserve inline DOM nodes, and tag groups need indivisible items. @re-dev/react-truncate picks the lightest measurement path that fits the current component and props.
Measurement Paths
How Each Path Works
Plain Text
When the content has no CJK characters, preserveMarkup is off, and word-break: break-all is not applied, <Truncate /> (along with <ShowMore /> and <MiddleTruncate />) reads the container content width once, syncs the computed font and letter-spacing to an offscreen Canvas context, and computes line breaks in memory via canvas.measureText() before committing a single render.
If CSS properties that Canvas cannot reproduce are active (such as text-transform, custom word-spacing, or font-variant), or if Canvas is unavailable in the environment, the component automatically falls back to a hidden DOM measurer without extra configuration.
CJK & break-all
Chinese, Japanese, and Korean text follow browser-native line-breaking and punctuation rules that simple space splitting cannot match accurately. Starting from v0.7.0, when using the default separator=" " with CJK characters—or when word-break: break-all is set—the component routes through browser-text to measure against real browser wrapping.
For large CJK lists where minimizing reflow matters more than native CJK punctuation rules, passing separator="" bypasses browser-text and stays on the pure Canvas path (see the benchmark numbers below).
Inline Markup
preserveMarkup is opt-in by design. When enabled, the component clones the rendered inline DOM into a hidden measurement container and binary-searches by line height so links, classes, and inline styles remain intact in the collapsed state. Enable it only when collapsed output needs live inline markup.
Item Groups
<TruncateGroup /> (added in v0.7.0) measures the bounding boxes (getBoundingClientRect) of its direct children and binary-searches for the longest prefix that fits within lines rows, rendering +N or a custom ellipsis for the rest. Because it operates on whole items, layout recalculations stay much lower than character-level DOM measurement.
Resize & Fonts
Starting from v0.7.0, container resizes and web font updates are handled automatically:
- Container resizing: Uses
ResizeObserveron the parent element (covering resizable panels and hidden-to-visible transitions) withflushSyncso re-measurement finishes before paint without multi-line flicker. Environments withoutResizeObserverfall back towindow.resize. - Web font loading: Listens to
document.fonts.readyandloadingdone, recalculating automatically once fonts settle even when container width does not change.
Performance Baseline
The numbers below come from the repository's real-browser benchmark suite (located in bench/, reproducible locally with pnpm bench), comparing internal paths alongside native CSS -webkit-line-clamp as a browser baseline.
Test Environment
- OS & Hardware: macOS (Darwin 27.3.0, arm64), Apple M3 Max (14 logical cores)
- Runtime & Browser: Node v22.19.0, Playwright Chromium 145.0.7632.6 (headless), viewport
1440 × 900 - Sampling: 2 warm-up rounds + 10 recorded rounds per scenario; continuous resize runs 50 steps from
640pxto320pxper round (500 pooled step samples)
First Mount
100 cards, in ms:
Note: LayoutCount and RecalcStyleCount were identical across all six scenarios and are shown together as Layout / Style.
Container Resize
20 cards resized across 50 steps, in ms per step:
Bundle Size
Zero runtime dependencies (dependencies is empty); only react and react-dom are declared as peer dependencies.
Practical Guidance
- Static multi-line clamping: If you do not need expand/collapse controls, custom ellipsis elements, middle truncation, or
onTruncatecallbacks, native CSS-webkit-line-clampremains the lightest option. - Large English / Latin feeds: The default Canvas path performs roughly 1 layout read per card on mount and stays at 60fps during 20-card continuous resize (6.56ms CPU per step).
- Large CJK feeds: Pair long lists with virtualization so only visible cards mount; if native CJK punctuation rules are optional, passing
separator=""reduces 100-cardLayoutCountfrom 2500 to 101. - Rich text and tag groups: Enable
preserveMarkuponly when collapsed output needs live inline markup, and use<TruncateGroup />for indivisible tags, avatars, or buttons.