From 394594d7723fbe9125a3294f19f604afc17789b9 Mon Sep 17 00:00:00 2001 From: Tanner Linsley Date: Fri, 13 Nov 2026 09:08:23 -0700 Subject: [PATCH] perf: reuse stable SVG sibling order --- .changeset/stable-svg-siblings.md | 5 ++++ packages/charts-core/src/reconcile.test.ts | 22 +++++++++++++++ packages/charts-core/src/reconcile.ts | 32 ++++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 .changeset/stable-svg-siblings.md diff --git a/.changeset/stable-svg-siblings.md b/.changeset/stable-svg-siblings.md new file mode 100644 index 00000000..36526aae --- /dev/null +++ b/.changeset/stable-svg-siblings.md @@ -0,0 +1,5 @@ +--- +'@tanstack/charts': patch +--- + +Avoid rebuilding child identity maps during non-animated SVG updates when sibling order is unchanged. diff --git a/packages/charts-core/src/reconcile.test.ts b/packages/charts-core/src/reconcile.test.ts index 4f539c6e..4c4c3ace 100644 --- a/packages/charts-core/src/reconcile.test.ts +++ b/packages/charts-core/src/reconcile.test.ts @@ -2,6 +2,28 @@ import { describe, expect, it, vi } from 'vitest' import { reconcileChartSvg, reconcileChartSvgFragment } from './reconcile' describe('keyed SVG reconciliation', () => { + it('preserves mixed keyed and unkeyed siblings through stable updates and reordering', () => { + const container = document.createElement('div') + const group = (value: number) => + `` + reconcileChartSvg(container, group(1)) + const children = [...container.querySelector('g')!.children] + reconcileChartSvg(container, group(2)) + expect([...container.querySelector('g')!.children]).toEqual(children) + expect(children[0].getAttribute('cx')).toBe('2') + expect(children[1].getAttribute('width')).toBe('2') + reconcileChartSvg( + container, + '', + ) + expect([...container.querySelector('g')!.children]).toEqual([ + children[1], + children[0], + children[2], + children[3], + ]) + }) + it.each([ ['old', 'new'], ['old', 'new'], diff --git a/packages/charts-core/src/reconcile.ts b/packages/charts-core/src/reconcile.ts index 67f02f6d..3c3e2b04 100644 --- a/packages/charts-core/src/reconcile.ts +++ b/packages/charts-core/src/reconcile.ts @@ -99,6 +99,38 @@ function reconcileElement( const currentChildren = [...current.children] const nextChildren = [...next.children] + // Stable sibling order needs no identity maps. Keep ambiguous keys, text, + // and animated updates on the general reconciliation path. + if ( + !tweens && + currentChildren.length === nextChildren.length && + current.childNodes.length === currentChildren.length && + next.childNodes.length === nextChildren.length + ) { + const seen = new Set() + let same = true + for (let index = 0; index < currentChildren.length; index++) { + const previous = currentChildren[index] + const following = nextChildren[index] + const key = previous.getAttribute('data-ts-key') + if ( + previous.namespaceURI !== following.namespaceURI || + previous.localName !== following.localName || + key !== following.getAttribute('data-ts-key') || + (key && seen.has(key)) + ) { + same = false + break + } + if (key) seen.add(key) + } + if (same) { + for (let index = 0; index < currentChildren.length; index++) { + reconcileElement(currentChildren[index], nextChildren[index], tweens) + } + return + } + } const currentByIdentity = indexChildren(currentChildren) const nextIdentities = identities(nextChildren) const unmatched = new Set(currentChildren)