I am building a code visualizer: you paste a Python or C++ program, it runs it, and you step through the execution with boxes, arrows and a variables panel. Replay happens entirely in the browser from a saved recording, so stepping back never re-runs your program.

The recording is the interesting part. A tracer takes a snapshot at every step, and each snapshot contains every live object. That is easy to write and it explodes: a linked list of 300 nodes recorded over 2,000 steps was 4.8 MB of JSON, and a 367-step run over forty small dictionaries was 6.7 MB. Both of those are small programs.

The first version of the format stored each step in full, like this:

{"step":3,"event":"line","line":3, "frames":[{"depth":0,"function":"<module>","line":3, "locals":{"head":{"kind":"ref","id":"o7"}}}], "heap":{"o7":{"type":"object","fields":{ "value":{"kind":"scalar","type":"int","value":42}, "next":{"kind":"ref","id":"o6"}}}}, "stdout":"","stderr":""} 

Look at what repeats. Between two steps, the heap rarely changes much, and most objects are identical. The file pays for them again on every step.

Store each object once

The fix is a pool. Keep an array of distinct heap objects, and let each step hold a map from object id to pool index:

node_pool: [ {...}, {...}, {...} ] events[3].heap: { "o7": 12, "o6": 11 } 

Two objects are the same object when their canonical text is the same. Canonical means keys sorted, recursively:

function canon(x) { if (x === null || typeof x !== 'object') return JSON.stringify(x); if (Array.isArray(x)) return '[' + x.map(canon).join(',') + ']'; const o = x; return '{' + Object.keys(o).sort() .map((k) => JSON.stringify(k) + ':' + canon(o[k])).join(',') + '}'; } 

That string is the Map key. No hashing, no digest, no chance of a collision, and no dependency. It is slower than a hash of the text, and for traces of this size that has not mattered.

The same trick applies to call frames, which repeat almost every step, and to stdout, which is cumulative: store the final text once and let each step keep a length into it. Lengths are counted in UTF-16 units, because that is what the viewer's string slice counts.

On the 367-step recording, 163 unique objects replaced the copies, and the file went from 6.68 MB to 0.39 MB. On the 2,000-step list it went from 4.82 MB to 1.20 MB.

Why the second case is worse: the list grows on almost every step, so nearly every step has a new version of the head node. Pooling exact copies helps less when objects keep changing.

Deltas for the changing ones

For those, a pool entry can be a delta of an earlier entry:

{"delta_of":41,"set":{"length":42},"append":[{"kind":"scalar","type":"int","value":7}]} 

Read it as: take entry 41, replace these top-level fields, add these items at the end. Items are never replaced, only appended, which is what makes growing lists and arrays cheap. The tracer picks the delta base as the last variant it saw of the same object id, so the chain follows the object's history rather than scanning the pool.

Verify before you trust it

A compact file that decodes to something slightly different is worse than a large file, because the bug shows up as a wrong picture, not a crash. So the writer does not trust itself. After encoding, it runs the result through the real reader and compares the rebuilt steps to the original:

const rebuilt = expandNodePool(JSON.parse(text), Infinity).events; ok = canon(rebuilt) === canon(trace.events); if (!ok) return { text: rawFull, pooled: false, reason: 'check failed, wrote the ordinary format' }; if (text.length >= rawFull.length) return { text: rawFull, pooled: false, reason: 'not smaller, wrote the ordinary format' }; 

If the check fails, or the compact form is not smaller, the user gets the ordinary file and a reason, not a broken one.

The reader is strict in the other direction too. It refuses unknown delta fields, forward or unknown delta bases, bad indexes, more than 10,000 events, and it caps how much a compact file may expand to at 20 MB. A hostile or damaged file cannot make the page build a billion objects.

What it costs

Encoding 367 steps took 487 ms in Node, and that includes the verification pass. The Python side got faster, not slower: writing each node once while tracing took 0.18 s against 0.26 s for the plain form, because the plain form re-serialises the same objects at every step.

Gzip is not the same thing

The obvious objection is that gzip already does this. Partly. Gzip removes repetition in the byte stream; pooling removes it before the bytes exist. On the 367-step file, plain gzip took 6.68 MB to 95.8 KB and the pooled file gzipped to 9.4 KB, about ten times smaller. On the 2,000-step list, plain gzip reached 60.5 KB and pooled gzip 44.3 KB, a much smaller gap. When objects mutate constantly, the pooled file has less exact repetition to exploit and gzip has less to remove. Both formats ship: plain JSON always saves, and the compact save is gzip on top.

What I would do differently

The pool is rebuilt on every save instead of being kept during recording in the browser. The Python tracer writes its pool while it traces; the frontend encoder replays the whole trace at save time. Doing it live in the browser would cut the 487 ms, and it would need the same round-trip check to stay trustworthy.

The lesson that transfers: for large ordered data in React, a boring model (pool, links, append-only deltas) plus a verification pass beats clever compression, because you can explain it and you can test it.

About the numbers: measured on one machine, Node 22.23.5 and Python 3.10.12, on three synthetic programs. Sizes are the compact JSON the app saves; the command-line tool writes the same recording pretty-printed, about 2.8 times the bytes, so a figure taken from a file on disk will look larger. Ratios depend on how much the traced program repeats, and a heap-heavy program will land between the two cases above.