Global variables
In browsers, the global object (window) is a GC root. Anything attached to it lives until you delete it or close the tab.
Common causes
Section titled “Common causes”- Assigning without declaration:
leaked = [](sloppy mode / implicit global) window.myCache = datafor debugging and forgetting- Libraries that attach to
window
flowchart TD window[window GC root] leakStore[leakStore array] chunk1[Chunk objects] chunk2[More chunks] window --> leakStore leakStore --> chunk1 leakStore --> chunk2
Broken code
Section titled “Broken code”function ingest(rows) { // Oops — no var/let/const in non-strict sloppy scripts buffer = buffer || []; buffer.push(...rows);}In modules (type="module") this throws ReferenceError — modules are strict. Leaks still happen via explicit window.x = ....
Fixed code
Section titled “Fixed code”const buffer = [];const MAX = 500;
function ingest(rows) { buffer.push(...rows); while (buffer.length > MAX) buffer.shift();}Or scope data to where it’s needed and let it go out of scope.
Live demo
Section titled “Live demo”Live demo: accidental globals
Idle — click Start to push objects onto an accidental global array.
`performance.memory` is only available in Chromium-based browsers. Use Chrome for live heap readouts.