Skip to content

Global caches

In Node, top-level module scope lives for the process lifetime — equivalent to a browser global.

See examples/node/01-global-array-leak.js:

const store = [];
setInterval(() => {
store.push(new Array(100_000).fill({ ts: Date.now() }));
logMemory('tick');
}, 500);

See examples/node/01-global-array-fixed.js — bounded ring buffer or periodic cleanup.

Terminal window
node examples/node/01-global-array-leak.js
# Ctrl+C after seeing heapUsed climb
node examples/node/01-global-array-fixed.js
xychart-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]