[Perf] Canvas layer composition is quadratic: splitRenderLayers re-copies the node array per node

Open Beginner friendly
#275 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
typescript

Research direction

Read packages/charts-core/src/canvas.ts around splitRenderLayers (lines 639–646) and inspect how it groups consecutive nodes by renderer. The issue provides a reproduction using a canvas dot mark and benchmark results; use that to check the cost before and after the change. Done means layer composition avoids repeated array copying and the reproduction no longer scales quadratically.

Written by the indexing model from the issue text.

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:

https://github.com/TanStack/charts/blob/b236dae2d57f1921b9e0296b6ecf023bf012bb5f/packages/charts-core/src/canvas.ts#L639-L646

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.

Dominant language
TypeScript
Stars
870
Forks
57
Avg merge
11h 58m
Merged PRs (30d)
18

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from TanStack/charts

All issues in TanStack/charts

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.