Global caches
In Node, top-level module scope lives for the process lifetime — equivalent to a browser global.
Broken code
Section titled “Broken code”See examples/node/01-global-array-leak.js:
const store = [];
setInterval(() => { store.push(new Array(100_000).fill({ ts: Date.now() })); logMemory('tick');}, 500);Fixed code
Section titled “Fixed code”See examples/node/01-global-array-fixed.js — bounded ring buffer or periodic cleanup.
Run it
Section titled “Run it”node examples/node/01-global-array-leak.js# Ctrl+C after seeing heapUsed climb
node examples/node/01-global-array-fixed.jsxychart-beta title "Global array leak vs bounded" x-axis [1, 2, 3, 4, 5, 6] y-axis "heapUsed MB" 0 --> 80 line "leak" [10, 22, 38, 52, 68, 74] line "fixed" [10, 18, 20, 19, 21, 20]