Mutation Observer
ELI5 — The Vibe Check
Mutation Observer is the surveillance camera for your DOM. It watches for changes - added nodes, removed nodes, attribute changes, text changes - and reports everything. It's how extensions, analytics, and third-party scripts detect what's happening on the page.
Real Talk
The Mutation Observer API watches for changes to the DOM tree, including child list modifications, attribute changes, and character data mutations. It batches notifications asynchronously for performance and replaces the deprecated Mutation Events API.
Show Me The Code
const observer = new MutationObserver((mutations) => {
mutations.forEach(m => console.log(m.type, m.target));
});
observer.observe(document.body, { childList: true, subtree: true });
When You'll Hear This
"MutationObserver is how analytics tools detect dynamic content" / "Use it sparingly - observing the whole DOM tree is expensive"
Related Terms
Intersection Observer
Intersection Observer watches elements and tells you when they enter or leave the viewport. It's the bouncer checking if components are visible.
Resize Observer
Resize Observer tells you when an element changes size. Not the window, the actual element.
Shadow DOM
Shadow DOM is like a force field around your component's internals.