Repository navigation
Description
splitRenderLayers in packages/charts-core/src/canvas.ts builds each layer by re-copying its node array once per node, so composing layers is quadratic in the number of consecutive nodes on one renderer:
charts/packages/charts-core/src/canvas.ts
Lines 639 to 646 in b236dae
| const append = (layer: ChartRenderLayer) => { | |
| const previous = layers.at(-1) | |
| if (previous?.renderer === layer.renderer) { | |
| previous.nodes = [...previous.nodes, ...layer.nodes] | |
| } else { | |
| layers.push(layer) | |
| } | |
| } |
const append = (layer: ChartRenderLayer) => {
const previous = layers.at(-1)
if (previous?.renderer === layer.renderer) {
previous.nodes = [...previous.nodes, ...layer.nodes]
} else {
layers.push(layer)
}
}It runs whenever a chart mixes renderers, for example one mark with renderer: canvasChartRenderer on the default SVG renderer. The function also recurses into every group to check for a custom renderer, including the default focus layer groups, which hold one node per point. So a canvas dot mark with n points costs about n²/2 element copies per render, on the very marks that were moved to canvas to make them cheap.
Reproduction
@tanstack/charts 1.5.0, Node 24 (vitest, jsdom). Median of 5 calls of the layered renderer's prerender:
import { createChartScene, defineChart, dot } from '@tanstack/charts'
import { canvasChartRenderer, createLayeredChartRenderer } from '@tanstack/charts/canvas'
import { scaleLinear } from '@tanstack/charts/scales/linear'
import { createSvgChartRenderer } from '@tanstack/charts/svg/renderer'
const layered = createLayeredChartRenderer(createSvgChartRenderer())
for (const n of [2000, 4000, 8000, 16000, 32000]) {
const data = Array.from({ length: n }, (_, i) => ({ x: i, y: (i * 7919) % 1000 }))
const chart = defineChart({
marks: [dot(data, { x: (d) => d.x, y: (d) => d.y, renderer: canvasChartRenderer })],
scales: { x: { scale: scaleLinear().domain([0, n]) }, y: { scale: scaleLinear().domain([0, 1000]) } },
})
const scene = createChartScene(chart, { width: 800, height: 400 })
const t0 = performance.now()
layered.prerender(scene, { ariaLabel: 'repro' })
console.log(n, performance.now() - t0)
}| points | now | with the fix below |
|---|---|---|
| 2,000 | 2.5 ms | 0.4 ms |
| 4,000 | 8.2 ms | 0.2 ms |
| 8,000 | 23.6 ms | 0.3 ms |
| 16,000 | 206 ms | 0.4 ms |
| 32,000 | 1,390 ms | 1.0 ms |
In a real app (a Vue Chart, a scatter chart of about 7,000 points with its points on canvas), wheel zoom spent about 300 ms per render in this function: about 51 million element copies per zoom step. That made the canvas version roughly ten times slower than plain SVG. With the fix, frames are about three times shorter than with SVG.
Suggested fix
Append in place. Every array append receives is a fresh literal built in this function, so mutating it is safe:
type BuildingLayer = Omit<ChartRenderLayer, 'nodes'> & { nodes: SceneNode[] }
const layers: BuildingLayer[] = []
const append = (layer: BuildingLayer) => {
const previous = layers.at(-1)
if (previous?.renderer === layer.renderer) {
for (const node of layer.nodes) previous.nodes.push(node)
} else {
layers.push(layer)
}
}(A loop rather than push(...layer.nodes), which can exceed the argument limit for very large layers.)
Found and measured with an AI coding agent (Claude Code); the numbers above are from real runs.
Activity
[-]Canvas layer composition is quadratic: splitRenderLayers re-copies the node array per node[/-][+][Bug] Canvas layer composition is quadratic: splitRenderLayers re-copies the node array per node[/+][-][Bug] Canvas layer composition is quadratic: splitRenderLayers re-copies the node array per node[/-][+][Perf] Canvas layer composition is quadratic: splitRenderLayers re-copies the node array per node[/+]tannerlinsley commentedon Nov 20, 2026
Thanks for the clear report and reproduction! There's a fix in #276 that builds each layer without repeatedly copying its nodes. This stays open while CI and delivery are still pending.