Demo · real data

A pivot table on a million rows, measured while you drag

Drag Country, Year, Month or Product between the areas. Every recompute is timed here, in your browser, next to the longest pause it caused on the page.

Built with the free PivotGrid, Community edition.

What this demo is

A pivot table over the Online Retail II dataset: 1,067,371 invoice lines from a UK online shop, December 2009 to December 2011, published by the UCI Machine Learning Repository under CC BY 4.0. Each line has a country, a date, a product, a quantity and a price. The page downloads the file once (about 6 MB), decodes it in your browser and hands the rows to the grid. Nothing is sent back; there is no server doing the sums.

It opens on revenue and units by country and year. Every other field waits in the filter area, so you can drag Month into the columns, put Product in the rows, or compare sales with cancellations by country. Three buttons above the grid start from those layouts.

What is being measured

A pivot table regroups and re-sums every row each time the layout changes. On a million rows the obvious implementation, one loop on the page's main thread, freezes the page while it runs: clicks wait, typing stalls. PivotGrid runs the aggregation in a Web Worker instead, so the main thread only draws the result.

The panel under the grid shows two numbers for every recompute: how long it took from the drag to the new table, and the longest pause on the main thread in that window, from the browser's own Long Tasks API, which reports any task of 50 ms or more. "Run it on the main thread" computes the same revenue by country and year with a plain loop and reports how long the page was frozen, with two of its totals next to the pivot's so you can check they agree. The text box is there to feel the difference: type while each one runs.

Wide layouts cost more than narrow ones. About 5,000 products by 25 months still pauses the page for tens of milliseconds on a fast machine, because drawing that many cells is real work; the panel shows what yours does. The article behind this demo has the measurements, including a slow sort we found here and fixed.

Build it yourself

Everything on this page is in the free Community edition of PivotGrid: dragging fields, sorting, filtering, number formats and the Web Worker. No licence key and no watermark, and commercial use is included. In React:

npm i @kanunilabs/pivotgrid-react

import { BasePivotGrid, type PivotField } from '@kanunilabs/pivotgrid-react';
import '@kanunilabs/pivotgrid-react/styles.css';

type Line = { country: string; year: string; month: string; revenue: number; quantity: number };

const fields: PivotField<Line>[] = [
  { id: 'country', dataField: 'country', caption: 'Country', dataType: 'string', area: 'row' },
  { id: 'year', dataField: 'year', caption: 'Year', dataType: 'string', area: 'column' },
  { id: 'revenue', dataField: 'revenue', caption: 'Revenue', dataType: 'number', area: 'data',
    summaryType: 'sum', format: { style: 'currency', currency: 'GBP', maximumFractionDigits: 0 } },
  { id: 'units', dataField: 'quantity', caption: 'Units', dataType: 'number', area: 'data', summaryType: 'sum' },
  { id: 'month', dataField: 'month', caption: 'Month', dataType: 'string', area: 'filter' },
];

export function SalesPivot({ rows }: { rows: Line[] }) {
  return (
    <div style={{ height: 560 }}>
      <BasePivotGrid gridId="sales" data={rows} initialFields={fields} />
    </div>
  );
}

The worker needs no setup: the package starts it itself. Not using React? The same grid is @kanunilabs/pivotgrid, driven from plain JavaScript.

Where to next