Reconciling two large CSV exports in the browser usually fails in one of three ways:

  1. Calling await file.text() on two 400 MB files blows past the tab heap limit before parsing even starts.
  2. Coercing cells with Number() or parseFloat() turns invoice ID "00123" into 123 and hides real mismatches.
  3. Indexing rows with map.set(key, row) silently overwrites duplicate keys—which in finance or order-vs-payment exports are usually the exact rows you needed to catch.

Here is the Web Worker architecture I ended up using to diff files up to 1 GiB locally with strict raw-string matching and bounded memory.

1. Stream chunks with file.stream() and a fatal TextDecoder

Instead of reading the whole file into memory, stream chunks off disk through ReadableStreamDefaultReader and decode them incrementally:

const reader = file.stream().getReader(); const decoder = new TextDecoder(encoding, { fatal: true }); 

Two details matter here if you want deterministic results on real exports:

  • { fatal: true }: If a file is actually Windows-1252 or GBK and you decode it as UTF-8, silent U+FFFD replacement characters will corrupt your diff keys. Failing fast lets the user switch source encoding before comparing.
  • Chunk boundaries: A \r\n sequence or a quoted multiline field can split right across two chunks. Track quoted, closed, and skipLF state across chunk iterations rather than splitting chunks by \n.

2. Guard against the V8 sliced-string heap trap

Even if you only keep 3 columns out of a 40-column CSV, slicing substrings out of a large decoded chunk in V8 creates SlicedString references that can pin the entire parent chunk string in memory.

Force-copying kept cell values (e.g. via a tiny copyText helper) detaches retained keys and values from the discarded chunk buffers. On top of that, track an explicit character budget (indexedUnits) as rows stream in so a user who selects 50 free-text columns gets a clear error instead of a frozen tab:

units += key.reduce((sum, v) => sum + v.length, 0); units += values.reduce((sum, v) => sum + v.length + 4, 0); if (units > compareLimits.indexedUnits) { throw Error('The selected columns exceed the memory budget. Compare fewer columns.'); } 

3. Never coerce keys—and never overwrite duplicates

Serialize the selected key tuple directly as raw strings (JSON.stringify(key)). Do not trim whitespace blindly and do not parse numbers:

  • "00123" stays "00123" and will not match "123".
  • Rows with blank key cells are pushed into a separate empty-key list instead of falsely matching each other.
  • Every key maps to a pair of arrays [rowsFromA, rowsFromB]:
const id = JSON.stringify(key); let group = groups.get(id); if (!group) { group = [[], []]; groups.set(id, group); } group[side].push({ record, values: values.map(copyText) }); 

During classification:

  • a.length === 1 && b.length === 1 → compare raw value tuples into Changed or Unchanged.
  • a.length === 1 && b.length === 0 → Only in A (and vice versa for Only in B).
  • a.length > 1 || b.length > 1 → route the entire key group into Needs review so duplicate IDs are never silently dropped.

4. Keep the Worker disposable

Because all reconciliation state lives inside the Worker, cancelling a job or switching files simply calls worker.terminate() and releases the entire heap immediately. For the exported report CSV, stream the classified rows into a Blob and revoke its Object URL on pagehide.

If you want to run this on a real export without writing a one-off script, I packaged this exact Worker into a small browser-only tool at fileminto.com/csv/compare (runs locally, no uploads). Curious how others handle large tabular diffs in the browser—especially when dealing with mixed encodings or composite keys.