/ 3 min read
A million rows in the browser: virtualisation, a worker and one numeric sort
The ledger demo on this site sorts, searches and scrolls a million rows without stutter. The four decisions that make it work: rows that are computed instead of stored, a sort in a worker, a scroll space the browser can hold, and a real grid for keyboards and screen readers.
In this article
Back office screens rarely need a million rows at once, but they often need to feel as if they could. The ledger in the lab is my test of that: a million transactions you can sort by any column, search by name or account, and move through with the keyboard. These are the decisions behind it.
1,000,000 rows · 21 in the DOM
Click a row or focus the table, then use ↑ ↓, Page Up, Page Down, Home and End.
1. Rows are computed, not stored
A million row objects would take hundreds of megabytes. The demo stores none. Every field of a row is computed from its row number with a small integer hash, so row 734,201 always reads the same, and only the forty or so rows on screen ever exist as objects.
Each sortable column also has a numeric key that keeps the column's natural order: names come from alphabetically sorted lists, so the index into the list sorts like the text itself. That detail is what makes the fast sort below possible.
2. Sorting off the main thread, with one numeric sort
Sorting a million items with a comparator function costs millions of calls into JavaScript. The worker avoids that by packing the key and the row number into a single double and using the typed array's native numeric sort:
const SHIFT = 1_048_576 // 2^20, above the row count
const packed = new Float64Array(ROW_COUNT)
for (let i = 0; i < ROW_COUNT; i++) packed[i] = sortKey(i, column) * SHIFT + i
packed.sort()
const order = new Uint32Array(ROW_COUNT)
for (let i = 0; i < ROW_COUNT; i++) order[i] = packed[i] % SHIFTEach column's ascending order is computed once and kept, so sorting the same column again, in either direction, is a walk over an array that already exists. Search runs in the same worker over that order, and the result travels back as a transferred buffer, not a copy.
3. A scroll space the browser can hold
Virtualisation is the familiar part: render only the visible rows plus a few above and below, and position them inside a tall scrolling box. The unfamiliar part is that a million rows at 40 pixels is 40 million pixels, and browsers cap how tall an element can be. Past the cap, the scrollbar simply stops.
The grid caps its scroll space at eight million pixels and scales: one pixel of scrollbar moves the list by a little more than one pixel of rows. The visible window is computed in "virtual" coordinates and shifted back into the real box, so the rows still sit exactly under the scrollbar thumb.
4. A real grid, not a pile of divs
A table that only works with a mouse is not finished. The ledger is an ARIA grid with a row count, row indexes on every rendered row and a selected row you can move with the arrow keys, Page Up and Down, Home and End. Jumping to row one million is a single key press.
An automated accessibility check caught one mistake here: the header row sat outside the element with the grid role, and the keyboard handler lived on an inner element. Both are fixed; the grid is now one grid with its rows inside, and keyboard focus is on the grid itself.
What carries over to real products
- Do not hold what you can compute or fetch per page.
- Move heavy work off the main thread before optimising it, then optimise it with the data types the platform is fast at.
- Test the extremes: the last row, the longest name, the empty search.
- Keyboard and screen reader support belong to the first version, not the last.
Try it live
Next article ↗
Moving routing out of the proxy: a function call on every page view