PP
← All posts

D3 at 10K nodes

May 1, 2026 · 12 min read

D3Data VizPerformance

Rendering 10K nodes at 60fps in a regulated banking SaaS, with the force simulation in a Web Worker, the rendering on canvas, hit detection without a quadtree, and zoom and pan that does not feel like Google Maps. Here is what survived contact with production.

The job

Marketing teams managed customer cohorts as CSVs. The relationship graph behind those cohorts was visible to the backend and nobody else. Product wanted a visual surface where marketing could see the network and pick outbound targets by their position in it.

The constraints: 10K live nodes at 60fps on a MacBook Air. Data in S3, streamed on demand. Zoom, pan, click, drag a node. Runs inside the React 18 platform shell, which we had just migrated.

SVG was off the table

SVG breaks past 5K nodes from DOM cost alone. Every node is a DOM element, every animation triggers a layout. At 10K nodes the browser spends more time reconciling than drawing. We measured. It was bad.

I tried it anyway because it was easier. The first prototype ran at 8fps on a clean machine. That ended the SVG conversation.

Canvas, then

Canvas is one element. Drawing is procedural. The browser does not reconcile a tree. The cost is linear in the number of nodes you draw. At 10K nodes the draw loop takes ~5ms per frame on the canvas alone, which left us 11ms for everything else inside a 16ms budget.

The downsides are real. Canvas has no built-in interaction. You build your own hit detection. You build your own zoom. You build your own selection state.

We chose canvas because the linear cost gave us a clear headroom story and the interaction code we had to write was a one-time investment.

Force in a Web Worker

The first useful trick is moving the force simulation off the main thread.

d3-force runs a simulation: each tick, it computes new positions for every node based on forces (charge, link, center, collide). With 10K nodes, a single tick takes 2-3ms on a fast machine and 8-10ms on a slow one. If the simulation runs on the main thread, it competes with React for the same 16ms budget. Result: dropped frames whenever React decides to render.

Move the simulation to a Web Worker and the main thread only paints. Position updates arrive via postMessage as a Float32Array (one number per coordinate, transferable buffer for zero-copy). The main thread reads the buffer and draws.

The buffer transfer pattern matters. If you postMessage an array of objects, the structured-clone cost dominates for large graphs. A Float32Array transferred via transferable buffers takes microseconds.

Hit detection without a quadtree

The textbook answer for "find which node is under the cursor" with 10K nodes is a quadtree. The textbook is right for some workloads. For this one, a naive linear scan turned out to be fine.

The reasoning: we only need hit detection on pointer events, which happen tens of times per second, not every frame. A linear scan of 10K nodes takes ~1ms. The quadtree would have shaved that to ~0.05ms at the cost of a tree rebuild on every simulation tick.

Quadtrees are correct when you have many hit tests per frame (collision detection between thousands of moving objects). For a UI graph with one cursor and one hit test per pointer event, linear is fine and the code is simpler.

If the graph ever moves to 100K nodes, the quadtree comes back. Today it does not.

Zoom and pan with d3-zoom

d3-zoom handles the math. You bind it to the canvas, it produces a transform matrix (translate x, translate y, scale), and you apply the transform inside your draw loop. The draw becomes:

ctx.setTransform(dpr, 0, 0, dpr, 0, 0)  // reset for device pixel ratio
ctx.clearRect(0, 0, width, height)
ctx.translate(transform.x, transform.y)
ctx.scale(transform.k, transform.k)
// draw nodes and edges in world coordinates

The trick is to set the device-pixel-ratio scale only once per frame and then apply the user transform on top. If you mix them, your line widths and font sizes lie to you.

Visual decisions

Node radius scales with degree. We used the square root of degree because a linear ramp made hubs visually overwhelming. The sqrt curve is more legible at the extremes.

Color ramps by degree, in OKLCH. OKLCH stays perceptually constant across lightness, which matters because the same segment color must read the same on a 2px peripheral node and a 16px hub. RGB ramps shift hue, and the user notices: "wait, are those the same segment?"

Edges are drawn before nodes, with low alpha and a thin line width that scales inversely with zoom. At wide zoom the edges fade into a haze. At close zoom they sharpen up. This is on purpose: at wide zoom you should see the topology, at close zoom you should see the relationships.

The IntersectionObserver pause

If the user scrolls past the graph, the simulation should not keep ticking. We wrapped the canvas in an IntersectionObserver. When the graph leaves the viewport, the simulation pauses. When it re-enters, it resumes. Same for tab visibility: the document.visibilitychange event pauses on hide.

This sounds obvious but it took us a week to wire because the first version paused the draw loop but kept the simulation running. The simulation kept burning CPU off-screen.

Touch and reduced motion

On touch devices we reduce the node count automatically (default drops to 500). Force simulations at 10K on a mid-range phone are not worth it; the topology is the point, not the count.

For users with prefers-reduced-motion, we run the simulation once, draw the final positions, and stop. They get the graph, they do not get the animation. This is a usability win and a CPU win.

What 100K would need

If we doubled the count, the simulation cost would go quadratic (force is O(n^2) without spatial acceleration). d3-force has a Barnes-Hut approximation for charge that brings it back to O(n log n). For 100K we would turn that on.

We would also move to WebGL. Canvas can draw 100K small circles in 16ms; that is the upper bound. WebGL ships the geometry to the GPU once and reuses it forever. The maintenance cost goes up: you write shaders, you handle context loss, you debug GPU memory. At 10K that is overkill. At 100K it pays for itself.

We would also add level-of-detail rendering: hide labels below a zoom threshold, hide edges below a degree threshold, draw only nodes in the viewport. These tricks compound.

What I would do differently

Add a WebGL fallback path with a feature flag, not a fork. We hit a customer with a 25K-node graph two months after launch. The canvas version held up at 30fps, which was acceptable but not great. WebGL behind a flag would have been ready.

Pre-bake the LOD strategy from day one, even if we turn it off. The hooks for hide-at-zoom and hide-by-degree are easy to add early and painful to retrofit.

The thing I keep coming back to

10K nodes at 60fps is not a hardware achievement. It is a series of correct boring choices: canvas instead of SVG, worker instead of main thread, transferable buffers instead of cloned objects, linear scan instead of premature quadtree, OKLCH instead of RGB. None of those are clever. Each one is the obvious answer to a question someone has already answered.

The clever part is being willing to skip the clever parts.

Next post

LLM chat UI primitives

Ten patterns I rebuilt across three internal chat surfaces. SSE contracts, structured output, retry, cancel, embedded BI.