Skip to content
← All writing

/ 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.

Live A million row ledger

1,000,000 rows · 21 in the DOM

1Sep 16, 16:35AC-042178Maya ReedTransferSettled$11,036.01
2Jul 25, 22:09AC-295881Jade StoneRefundSettled$4,340.92
3Oct 13, 12:29AC-182531Ana HuntDepositSettled$9,861.31
4Aug 22, 17:49AC-023426Maya BrooksPaymentSettled$15,478.15
5Apr 04, 05:17AC-530947Jade FordDepositSettled$14,557.43
6Sep 28, 20:28AC-779456Noah HayesTransferSettled$18,357.62
7Jan 04, 20:43AC-214219Dan ReedDepositSettled$9,379.70
8Sep 05, 16:55AC-456553Mia HayesRefundSettled$9,000.03
9Aug 12, 05:00AC-930434Ana AdamsRefundSettled$11,269.51
10Jul 09, 16:30AC-749906Omar DiazTransferSettled$2,978.09
11Oct 03, 11:28AC-033489Sam RossPaymentSettled$13,969.18
12Aug 19, 12:55AC-635720Dan HayesDepositSettled$6,913.91
13Mar 16, 11:52AC-783517Maya GrayPaymentSettled$14,409.16
14Jun 30, 04:19AC-123606Kai BrooksRefundSettled$1,535.53
15Jul 12, 20:22AC-891273Leo GrayFeeSettled$16,192.15
16Apr 30, 22:34AC-329427Mia LaneFeeSettled$4,102.62
17Mar 01, 12:25AC-897251Ben NovakPaymentPending$2,237.66
18Apr 16, 13:48AC-922819Ruby BrooksPaymentSettled$8,416.23
19Jul 15, 15:08AC-796806Omar FordFeeSettled$13,447.72
20Apr 07, 00:19AC-679860Mia HayesTransferSettled$16,701.49
21Mar 18, 05:23AC-308440Eli MossDepositSettled$17,834.15

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] % SHIFT
The key decides the order; the low bits carry the row number back out.

Each 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.

Next article ↗

Moving routing out of the proxy: a function call on every page view