IntersectionObserver and Asynchronous Visibility
IntersectionObserver notifies your callback asynchronously when a target element crosses a visibility threshold relative to the viewport or a scroll root. Because the engine reads cached boxes after Layout in the Rendering Pipeline, it eliminates scroll-handler Layout Thrashing.
scroll + getBoundingClientRect() vs IntersectionObserver
window.addEventListener('scroll', () => {
cards.forEach((c) => {
const rect = c.getBoundingClientRect(); // Forces sync Layout
if (rect.top < innerHeight) loadImage(c);
});
});Reads layout geometry on every scroll event, causing Layout Thrashing.
const io = new IntersectionObserver((entries) => {
for (const e of entries) if (e.isIntersecting) { loadImage(e.target); io.unobserve(e.target); }
}, { rootMargin: '200px' });
cards.forEach((c) => io.observe(c));Piggybacks on the browser's post-layout frame pass and fires only on threshold crossings.
Under the Hood
Blink evaluates IntersectionObserver registrations during Update the Rendering immediately after Layout, reusing already-computed box geometry instead of forcing synchronous getBoundingClientRect() reflows. Like a warehouse guard checking monitors once after each shift change, it fires callbacks only when intersectionRatio crosses a configured threshold.
- Post-Layout Hook: During
Update the Rendering, the engine reuses already-computed layout boxes to test intersections. - Threshold Crossing: Callbacks fire only when
intersectionRatiocrosses a configured threshold (not on every pixel scrolled). - One Observer, Many Targets: A single
IntersectionObserverinstance tracks thousands of elements more cheaply than one observer per component.
const io = new IntersectionObserver(
(entries) => {
for (const entry of entries) {
if (!entry.isIntersecting) continue;
hydrateWidget(entry.target);
io.unobserve(entry.target);
}
},
{ rootMargin: '200px 0px', threshold: 0.01 }
);Problem Genealogy
- 2010 (
scroll+getBoundingClientRect()): Ad-visibility trackers and lazy-loaders caused catastrophic main-thread reflows. - 2016 (
IntersectionObserverv1): Moved visibility math into the browser's post-layout step. - 2020 (
IntersectionObserverv2): AddedtrackVisibilityanddelayfor occlusion-aware ad-fraud checks.
Production 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.
- When not to use
IntersectionObserver: Never use it for continuous pixel-by-pixel parallax or scroll-linked animations, use CSSanimation-timeline: scroll()orview().
Connections & Logical Next Steps
IntersectionObserver computes intersections once per frame immediately after the Layout phase.
Replaces scroll-handler getBoundingClientRect() polling that forces synchronous layout recalculation.
MutationObserver tracks DOM tree/attribute edits as microtasks; IntersectionObserver tracks post-layout geometry.