DOM & EventsBaseline · Widely

IntersectionObserver and Asynchronous Visibility

Mental Model

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.

Interactive Simulator

scroll + getBoundingClientRect() vs IntersectionObserver

Lines of Code
Before
6
After
4
Reflows / Browser Cost
Before
Forced sync reflow/tick
After
0 forced reflows
BeforeScroll Listener + getBoundingClientRect()
window.addEventListener('scroll', () => {
  cards.forEach((c) => {
    const rect = c.getBoundingClientRect(); // Forces sync Layout
    if (rect.top < innerHeight) loadImage(c);
  });
});
Lines: 6Reflows: Forced sync reflow/tickDependencies: Main-thread scroll jank

Reads layout geometry on every scroll event, causing Layout Thrashing.

After (Native)Single Shared IntersectionObserver
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));
Lines: 4Reflows: 0 forced reflowsDependencies: Batched post-layout

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.

  1. Post-Layout Hook: During Update the Rendering, the engine reuses already-computed layout boxes to test intersections.
  2. Threshold Crossing: Callbacks fire only when intersectionRatio crosses a configured threshold (not on every pixel scrolled).
  3. One Observer, Many Targets: A single IntersectionObserver instance tracks thousands of elements more cheaply than one observer per component.
JS
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 (IntersectionObserver v1): Moved visibility math into the browser's post-layout step.
  • 2020 (IntersectionObserver v2): Added trackVisibility and delay for occlusion-aware ad-fraud checks.

Production Pitfalls

Production Pitfall
  • 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
  • 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().

Connections & Logical Next Steps

DOM & EventsIntersectionObserver and Asynchronous Visibility
What This Unlocks
Leaf concept

Mechanical Comparisons