Invoice visualizer: half a million invoices, in the browser
- React 19
- TypeScript
- Web Workers
- TanStack Virtual
- pdf-lib
- pdf.js
- Radix UI
The use case
A finance team needs to search, sort and export invoices at volume, and read the PDFs afterwards, without waiting on a server or watching the tab freeze.
Measured
- to generate 500,000 invoices
- 0.2 s
- worst frame while scrolling 500,000 rows
- 17 ms
- to search and filter 500,000 invoices
- 6 ms
- to import a 29 MB, 500,000-row CSV
- 0.9 s
- to build a 5,000-page PDF
- 4.7 s
- to open a 47 MB, 20,000-page PDF
- 1.5 s
Measured in a production build with headless Chromium on a Mac. Other machines will differ; the page itself shows live timings as you use it.
What I built
- Kept the data out of React. Invoices sit in typed columns inside a web worker, so the page holds only the rows on screen (page memory stayed around 10 MB at 500,000 invoices).
- Virtualized the table: about 25 to 40 rows exist at any time, fetched from the worker in small chunks, with a full keyboard model (arrows, Home and End, Space to select, Shift for ranges).
- Made filtering cheap: each column is sorted once and kept, so a filter or a search is one pass over the data in the worker, and the page never blocks.
- Streamed CSV import with column matching for unfamiliar headers, and a clear list of the rows that were skipped and why.
- Built PDFs in a worker, page by page, with progress and a cancel that stops at once. The previewer reads byte ranges from the file and draws only the pages near the viewport, so file size barely matters.
- Loaded the heavy parts only when used: the route is about 23 KB gzipped, pdf.js (about 100 KB gzipped) arrives when you first open a PDF.
Decisions worth explaining
Data in a worker, not in React state
Half a million invoices as objects would cost hundreds of megabytes and make every update expensive. Typed columns (numbers, dates as days, dictionary-coded names) cost tens of megabytes and sit in a worker, so React only ever holds what is on screen.
Sort once per column, then filter in one pass
Sorting on every keystroke would be too slow. Each column is sorted once, on first use, and kept as an index list. A filter then walks that list and keeps the matches, so the order comes for free.
Rows arrive after the scroll, so the table shows placeholders
The worker returns only the order of rows. The few rows in view are fetched in small chunks and cached, and a row not yet fetched shows a quiet skeleton. Scrolling never waits for data.
Cancel by ending the worker
A PDF build is one long call inside a library. Rather than poll for a cancel flag, the page ends the worker, which stops it at once and returns its memory.
Read the PDF by byte range
Large PDFs are never read whole. The viewer gives pdf.js a range reader over the file, so only the parts it asks for are loaded, and only the pages near the viewport are drawn.
Limits, on purpose
Browsers cap how tall one scrolling element can be, so the table stops at 500,000 rows; a single export is limited to 5,000 invoices. The invoice layout is shared by the on-screen preview and the PDF, so what you see is what you export.