# Performance and large diagrams

> How the diagram stays fast with thousands of nodes, what overview mode is and when it switches on, and how to tune large graphs.

Birch UI Diagram stays responsive with 5,000 nodes and 7,000 edges. Three techniques do most of the work: only what is near the viewport is in the DOM, routes are cached and computed within a frame budget, and very dense views switch to a single-path overview drawing.

## Virtualization

Nodes and edges are rendered only when they are inside the viewport or within a margin around it. A uniform-grid spatial index answers "what is in this rectangle" and "what is under the pointer" without scanning the whole model, so panning a large diagram mounts and unmounts a few elements per frame instead of touching thousands.

The background grid is a single SVG pattern, and the minimap draws all nodes as one path, so neither grows with the diagram.

`SpatialIndex` is exported if you build overlays of your own and need the same fast rectangle queries:

```ts
import { SpatialIndex } from '@birchui/diagram';

const index = new SpatialIndex(256);
for (const node of diagram.getNodes()) {
  index.insert(node.id, { x: node.x, y: node.y, width: node.width ?? 160, height: node.height ?? 64 });
}
const inRegion = index.query({ x: 0, y: 0, width: 1200, height: 800 });
```

It also has `update`, `remove`, `queryPoint` and `nearest`.

## Overview mode

When more nodes would be visible than `overviewThreshold` (default 1,200), typically after zooming far out on a big graph, the scene switches to an **overview drawing**: one SVG path for all node outlines, one for all edges as straight center-to-center lines, and one for the selected nodes. That is about three elements in total, however many nodes there are.

In overview mode:

- labels, ports, templates and routed paths are not drawn, and the root element has the class `lq-diagram--overview`;
- panning, zooming and the minimap keep working, and selected nodes stay highlighted;
- zooming back in below the threshold restores the full drawing automatically.

```ts
createDiagram(container, { nodes, edges, overviewThreshold: 600 });
```

Lower the threshold when your node templates are heavy (images, nested HTML), so the switch happens before the browser struggles. Raise it on fast machines with simple built-in shapes.

## Routing budget

Orthogonal routing is the most expensive part of rendering. Routes are cached and recomputed only for edges whose ends moved or whose corridor changed. Visible edges are routed within about 12 ms per frame; an edge that is not routed yet is drawn straight for a moment and refined on the following frames. See [Routing](https://docs.birchui.com/diagram/routing.md#performance).

For very dense graphs, `router: 'straight'` (per edge, or `defaultEdge: { router: 'straight' }`) costs almost nothing to draw.

## Measured timings

The package's performance tests run in happy-dom on a laptop. These are generous upper bounds for the engine itself; real browsers add layout and paint:

| Scenario | Time |
| --- | --- |
| Mount 5,000 nodes and 7,000 edges | ~120 ms |
| Pan frame at 100% with that model | ~15 ms |
| Drag frame with that model | ~20 ms |
| Zoom to fit (overview frame) | ~55 ms |
| Layered layout of 1,000 nodes and 1,500 edges | ~50 ms |
| Orthogonal routing of 500 edges among 400 obstacles | ~50–70 ms |

The bundle is about 41 KB minified and gzipped, plus 2.7 KB of gzipped CSS.

## Guidelines for large graphs

- **Load once.** Pass the whole model at creation or with `fromJSON` rather than calling `addNodes` thousands of times; each API call is a history step.
- **Batch API edits.** `addNodes`, `removeNodes`, `addEdges` and `removeEdges` accept arrays and record one step for the batch.
- **Store coordinates.** Run [auto-layout](https://docs.birchui.com/diagram/layout.md) once, on the server with `runLayout` if you like, and save the positions, instead of laying out on every load.
- **Prefer `layered` or `tree`** for big graphs. `force` is best below a few hundred nodes.
- **Keep templates light.** A template runs whenever its node changes; avoid images that reload and heavy DOM in nodes that are moved often.
- **Listen narrowly.** `nodesChange` and `edgesChange` payloads are built only while someone listens; debounce saves (see [Saving to a backend](https://docs.birchui.com/diagram/serialization.md#saving-to-a-backend)).
- **Use the list view** for screen-reader users on very large diagrams; it presents the structure without the canvas. See [Keyboard and accessibility](https://docs.birchui.com/diagram/accessibility.md#list-view).

## Next steps

- [Layout](https://docs.birchui.com/diagram/layout.md) for laying out big graphs on a server.
- [Interaction](https://docs.birchui.com/diagram/interaction.md#pan-zoom-and-fit) for zoom limits.
