[Perf] Tooltip: every move re-renders the React chart, builds unused default content and rewrites unchanged attributes #187
Version: @tanstack/charts 1.1.0 (57774e1).
With a custom tooltip body (and with the motion() renderer for F), each pointer move that changes the tooltip does work no one sees: the React adapter re-renders the chart (A), core builds the default content the custom body replaces (E), and paint rewrites attributes that did not change (F). Related: #179 (only the default tooltip duration).
Paths are under packages/; links pin commit 57774e14a1a86081eddb3a23523724a40da05640.
A. Tooltip target state re-renders the chart (react-charts)#
useTooltipBodykeeps the target inReact.useState;onChangecallssetTarget(tooltip.tsx:152-159).- Each render rebuilds
hostOptions, so the layout effect callsadapter.update(RendererChart.tsx:162-186).updaterefreshes DOM text metrics (renderer.ts:985-986). - Repro (vitest + jsdom, 4 pointer moves):
expected 3 to be +0calls toadapter.update. - Expected: a target change updates only the tooltip body. Proposed: keep the target in an external store read by a leaf component (
useSyncExternalStore). - Downstream measurement (bklit migration, Chrome, core
Chart+ external store): pointermove p95 2-6x lower.
B. A repeated hide() restarts the exit#
Moved to #209 (a bug, together with C).
C. Finished tooltip animations stay attached#
Moved to #209.
D. First show cannot be styled or delayed#
Moved to #210 (a feature, together with the per-phase
motionproposed under B).
E. Default content is always built#
paintalways callscreateChartTooltipContentbefore the custom body (tooltip.ts:117-124), and the result feeds thearia-label(:293).- Repro: an
items[].textformatter is called 1 time per paint with a custom body, expected 0. - Expected: default content computed only when read. Proposed: resolve
contentlazily, or let a custom body supply the accessible label. - Downstream measurement (bklit migration, Chrome CDP profile, 300 moves): about 6 ms per 100 moves; grows with series count.
F. Each paint rewrites the tooltip element (from this comment)#
- On every move that repaints the tooltip,
paintsetsvisibility: hiddenand removes it again, and re-setsclassName,pointer-events,user-selectanddata-sticky, even when nothing changed (tooltip.ts:106-162).configureTooltipSemanticsre-setsrole,aria-liveandaria-label. - The spring glide writes
style.translatefromrequestAnimationFrameevery frame (motion.ts:419-428, motion.ts:484-497). The tween glide uses WAAPI instead. - Measured (production build, sankey,
MutationObserveron the tooltip, 60 moves): about 12 mutation records per move. About 4.7 aretranslate, 2.5visibility, 3.75 the unchanged attributes, 1.8 content and 1left/top. - Proposed: compare before writing in
paint. Run the spring glide as a sampled WAAPItranslate, like the tween path.
toomingos(GitHub profile, opens in a new tab) changed the title
Thanks @toomingos. Part A is fixed in #234, which has merged but is not on npm yet. Custom tooltip target state now stays in the tooltip child instead of rerendering the React chart host. The regression tests fail on the old implementation with three extra adapter updates, and the fix passed ordinary and StrictMode tests plus packed-package lifecycle checks on React 18.0, 18.3 and 19.2 in Chromium, Firefox and WebKit. The scene renderer already avoided redraws before this fix, so this is specifically a React host update improvement. Keeping this issue open for the remaining content construction and DOM-write work.
The React host update fix from #234 is now published in 1.2.0 through #220. Thanks again, @toomingos. I verified the published React and core package versions and downloaded tarball integrity.
This ships part A only. Keeping the issue open for the default-content construction and repeated DOM-write work in E and F.
Thanks @toomingos. A small part of F is now available in 1.4.0 through #254: plain-text tooltips no longer clear their accessible label twice during the same paint. Keeping this open for default-content construction and the remaining repeated DOM writes.
AI review notes: The downloaded 1.4.0 core artifact matches npm's SHA-512 integrity and contains the narrow label cleanup. This does not make custom content lazy or remove all tooltip mutations.
Sign in to join this conversation.
F. Each paint rewrites the tooltip element#
Checked against
57774e14a1a86081eddb3a23523724a40da05640(v1.1.0).paintsetsvisibility: hiddenand removes it again, and re-setsclassName,pointer-events,user-selectanddata-sticky, even when nothing changed (tooltip.ts:106-162).configureTooltipSemanticsre-setsrole,aria-liveandaria-label.style.translatefromrequestAnimationFrameevery frame (motion.ts:419-428, motion.ts:484-497). The tween glide uses WAAPI instead.Measured (production build, sankey,
MutationObserveron the tooltip, 60 moves): about 12 mutation records per move. About 4.7 aretranslate, 2.5visibility, 3.75 the unchanged attributes, 1.8 content and 1left/top.Proposal: compare before writing in
paint. Run the spring glide as a sampled WAAPItranslate, like the tween path.