# Performance

> What the grid re-renders, how to observe it in development, and the budgets that gate every release.

Canonical: https://salmonui.dev/docs/data-grid/performance

Performance comes first in Salmon UI: every performance
budget is a release gate, asserted on a
developer or reference machine by `npm run release:verify` (there is no
hosted CI), and a behavior that cannot meet one ships opt-in or not at
all. Virtualization,
scaled scrolling and the server-rendered range are on the
[virtualization](https://salmonui.dev/docs/data-grid/virtualization) page; this page covers what one
interaction re-renders.

## Compiled columns

Columns compile once per stable definition identity (Structural
Budgets, "Column compile count for a stable `columns` prop identity": 1 for
the component lifetime). Keep `columns` at module scope or in a memo: a
parent re-render then passes the same identity, and the grid re-renders no
header and no cell. Reactive fields (`hidden`, numeric `width`, `pinned`,
order) are applied as runtime state when they change, and a changed
definition recompiles only that column. The compile count itself is gated
by the core unit test that reads `engine.getInstrumentation()`
(`createDataGridEngine({ instrument: true })`); on `DataGrid`, the initial-only
`instrument` prop does the same (`api.engine.getInstrumentation()`), and
leaving it off costs nothing.

## One million local rows

A `data` array of 1,000,000 rows needs no special mode.
Two budgets gate it: the client mount to the first painted viewport
of the 1M × 20 `million` fixture takes at most 500 ms in a production
build, and engine creation plus the first viewport read at most 500 ms and
10 ms. Rows are never copied or wrapped, and only the rendered window is
read. The [One million rows demo](https://salmonui.dev/demos/data-grid/million-rows) on the
[virtualization](https://salmonui.dev/docs/data-grid/virtualization) page mounts that data set and
scrolls it end to end.

## Selection and focus

Data Grid state lives in the core engine store; React components subscribe to
narrow slices. Moving the focus re-renders at most the old and the new cell
(the demo's arrow-key moves commit nothing at all), and toggling one row's
selection re-renders that row's cells plus the select-all header cell,
never the rest of the grid (Structural Budgets, "Cells re-rendered
on focus move" ≤ 2 and "Cells re-rendered on single-row selection toggle").
The selection toggle itself is O(1) in the core (Timing Budgets,
Interaction).

## What a grid downloads

A grid downloads only the features its props turn on. A plain
`<DataGrid data columns />` loads the base grid (rendering, virtualization,
sorting, filtering, selection, keyboard), which the Minimal app budget
(Bundle Budgets) caps at 110 KB gzip of initial JS with React and React DOM
excluded. Setting `grouping`, `paging`, `search`,
`toolbar`, `aggregates`, `renderDetail` or `childGrid`, a lookup column, and
the other feature props loads that feature's code on demand; each one has
its own size budget.

- Server rendering is unchanged: the server has every feature's code, so the
  HTML is complete, and hydration keeps it on screen until the browser has
  loaded the features it needs.
- A grid rendered only in the browser shows a placeholder of its own size
  (`aria-busy`) for that one download.
- Editors, menus, dialogs, export and print load later, on first use.

To skip the wait, pass the features to the optional `features` prop. It
bundles them with your page; it does not enable anything, so the feature
props are still required:

```tsx
import { grouping, paging } from '@salmonui/data-grid/features';

<DataGrid data={orders} columns={columns} grouping paging features={[grouping, paging]} />;
```

Leaving `features` out never disables a feature; the feature's code is then
loaded on demand instead.

## Observing renders in development

Development builds call `globalThis.__SALMON_UI_DEVTOOLS__.onCommit(commit)`
after every grid commit with the re-rendered cell, row and header ids
(`DataGridDevtoolsCommit`); production builds never reference the hook. The
demo installs it in an effect and shows the totals since the last reset in
a separate component, so counting never re-renders the grid.

### Live demo: Render counts

Live demo: https://salmonui.dev/demos/data-grid/render-counts

The dev-only __SALMON_UI_DEVTOOLS__ hook counts what each interaction re-renders - nothing for a parent re-render with stable columns, only the touched cells for focus and selection.

`Demo.tsx`:

```tsx
'use client';

import { useEffect, useState, useSyncExternalStore } from 'react';
import { Button } from '@salmonui/buttons/button';
import { DataGrid, type DataGridColumn, type DataGridDevtoolsCommit } from '@salmonui/data-grid';
import { createOrders, type Order } from '../../lib/fixtures';

const orders = createOrders(10_000, 97);

// A stable `columns` identity: compiled once for the grid's lifetime.
const columns: DataGridColumn<Order>[] = [
  { field: 'id', header: 'Order', primaryKey: true, width: 110 },
  { field: 'customer', header: 'Customer', width: 150 },
  { field: 'country', header: 'Country', width: 120 },
  { field: 'product', header: 'Product', width: 140 },
  { field: 'amount', header: 'Amount', type: 'number', format: 'C2', align: 'end', width: 120 },
];

interface Totals {
  readonly commits: number;
  readonly cells: number;
  readonly rows: number;
  readonly headers: number;
}
const ZERO: Totals = { commits: 0, cells: 0, rows: 0, headers: 0 };

/** Render totals since the last reset, fed by the dev-only `__SALMON_UI_DEVTOOLS__` hook. */
function createCounter() {
  let totals = ZERO;
  const listeners = new Set<() => void>();
  const emit = () => listeners.forEach((listener) => listener());
  return {
    record(commit: DataGridDevtoolsCommit) {
      totals = { commits: totals.commits + 1, cells: totals.cells + commit.cells.length, rows: totals.rows + commit.rows.length, headers: totals.headers + commit.headers.length };
      emit();
    },
    reset() {
      totals = ZERO;
      emit();
    },
    get: () => totals,
    getServer: () => ZERO,
    subscribe(listener: () => void) {
      listeners.add(listener);
      return () => void listeners.delete(listener);
    },
  };
}
type Counter = ReturnType<typeof createCounter>;

/** The read-out re-renders on its own, so counting never re-renders the grid. */
function Readout({ counter }: { counter: Counter }) {
  const totals = useSyncExternalStore(counter.subscribe, counter.get, counter.getServer);
  return (
    <p className="demo-status" role="status" aria-label="Render counts">
      Since reset: {totals.commits} grid commits, {totals.cells} cells, {totals.rows} rows, {totals.headers} header cells re-rendered.
    </p>
  );
}

export default function RenderCountsDemo() {
  const [counter] = useState(createCounter);
  const [parentRenders, setParentRenders] = useState(0);
  useEffect(() => {
    // Development builds only: the grid reports each commit to this global hook.
    const target = globalThis as { __SALMON_UI_DEVTOOLS__?: { onCommit?(commit: DataGridDevtoolsCommit): void } | undefined };
    const previous = target.__SALMON_UI_DEVTOOLS__;
    target.__SALMON_UI_DEVTOOLS__ = { onCommit: (commit) => counter.record(commit) };
    return () => {
      target.__SALMON_UI_DEVTOOLS__ = previous;
    };
  }, [counter]);
  return (
    <>
      <div className="demo-toolbar">
        <Button variant="outline" onClick={counter.reset}>
          Reset counters
        </Button>
        <Button variant="outline" onClick={() => setParentRenders((n) => n + 1)}>
          Re-render the parent ({parentRenders})
        </Button>
      </div>
      <DataGrid<Order> aria-label="Orders with render counting" data={orders} columns={columns} height={320} locale="en-US" currency="USD" timeZone="UTC" />
      <Readout counter={counter} />
    </>
  );
}
```

## API

`instrument`, `recordCommands` and `features` are on
[`DataGridProps`](https://salmonui.dev/docs/reference/data-grid/DataGridProps); the
feature values come from
[`@salmonui/data-grid/features`](https://salmonui.dev/docs/reference/data-grid/features).
