IntersectionObserver and Asynchronous Visibility vs MutationObserver and Batched Microtask Delivery
MutationObserver tracks DOM tree/attribute edits as microtasks; IntersectionObserver tracks post-layout geometry.
Structured Comparison Matrix
| Dimension | IntersectionObserver and Asynchronous Visibility | MutationObserver and Batched Microtask Delivery |
|---|---|---|
| Mental Model | Eliminate scroll-handler layout thrashing in lazy loading and infinite lists—IntersectionObserver reads cached layout boxes once per frame after Layout. | Replace main-thread-freezing Mutation Events with batched microtask delivery—MutationObserver queues DOM edits silently and flushes MutationRecord[] once after the Call Stack unwinds. |
| Maturity & Domain | Baseline · Widely · DOM & Events Chrome 58+ · Firefox 55+ · Safari 12.1+ | Baseline · Widely · DOM & Events Chrome 26+ · Firefox 14+ · Safari 6+ |
| Prerequisites | ||
| When Not to Use | When not to use IntersectionObserver: Never use it for continuous pixel-by-pixel parallax or scroll-linked animations, use CSS animation-timeline: scroll() or view(). | When not to use MutationObserver: Never use MutationObserver to detect element size or viewport visibility, use ResizeObserver or IntersectionObserver. |
Engine Mechanics & Trade-Offs
IntersectionObserver reads already-computed layout boxes inside the rendering pipeline and delivers batched visibility deltas asynchronously without triggering synchronous reflow.
Key Pitfalls: One Observer Per List Item: Instantiating new IntersectionObserver() inside every React card wastes memory; share one observer per threshold config. Forgetting unobserve(): Leaving lazy-load observers attached after hydration keeps firing callbacks on scroll.
MutationObserver batches all synchronous DOM mutations into a MutationRecord array and invokes its callback as a single microtask before the next render.
Key Pitfalls: Self-Mutation Microtask Loops: Writing an attribute inside a MutationObserver callback watching that same attribute starves the render loop forever. subtree: true on document.body: Watching the whole document floods your callback during framework hydration; pair with IntersectionObserver for visibility instead.