Detached DOM
A detached DOM node is an element no longer in the document that is still reachable from JavaScript. The browser cannot free that subtree until your last reference is gone.
Detailed Explanation
The document tree is a root. A node inside document is reachable from the page. element.remove() or parent.innerHTML = "" takes the node out of the tree. If nothing in JavaScript points at it, the node and its descendants become garbage, along with their listeners.
If you still have a variable, a closure, a Map, or a jQuery-style cache pointing at that node (or at a child), the whole subtree stays. DevTools names this Detached HTMLElement in a heap snapshot. A list of 500 removed rows, each with a click handler that closed over a row model, is a real leak in long-lived SPAs.
Common retainers:
const el = document.getElementById("panel")kept in a module after you removed#panel.- A listener on
documentwhose callback closes overel. MutationObserverthat was notdisconnect()ed.- A
WeakMapis not a retainer of the key; aMapis.
innerHTML on a parent destroys descendant nodes and their listeners, but any JS reference to those descendants still holds them. Replacing a view by assigning innerHTML without dropping your old button variable is a leak plus a bug (you click a node that is not on screen).
Prevention: scope element references to the component's life. On teardown, removeEventListener, observer.disconnect(), abort() fetches, and set el = null if the variable lives longer than the node. Frameworks do this in unmount hooks; vanilla code does it by hand.
What it costs you: heap growth as the user navigates. Detached trees include images, layout data, and listener closures. A snapshot after "list → detail → list" ten times should not show ten copies of the list.
Reach for the Memory panel's "Detach" filter when a SPA feels heavier after use. Reach for WeakMap when you attach metadata to a node you do not own, so the metadata dies with the node.
Code Examples
function mount(root) {
const button = document.createElement("button");
button.textContent = "ok";
const onClick = () => console.log("clicked");
button.addEventListener("click", onClick);
root.append(button);
return function unmount() {
button.removeEventListener("click", onClick);
button.remove();
};
}Leak: module keeps the node after remove()
// Leak: module keeps the node after remove()
let leaked;
function show(root) {
leaked = document.createElement("div");
leaked.textContent = "panel";
root.append(leaked);
}
function hide() {
leaked.remove(); // still referenced by `leaked`
}const notes = new WeakMap();
function tag(node, text) {
notes.set(node, text); // does not keep `node` alive
}Common Gotchas
`remove()` does not drop your variables
the node is only out of the document. Null the reference or let it leave scope.
Listeners on `document`/`window` outlive a view
they keep everything the handler closed over, including detached nodes.
`Map` keyed by nodes pins those nodes
use `WeakMap` for per-node metadata.
`innerHTML = ""` drops children from the tree, not from JS
leftover child references still leak.