Skip to content

Lesson 7 of 9 · 19m

Observers: Let the Browser Tell You What Changed

IntersectionObserver, ResizeObserver, MutationObserver, and PerformanceObserver replace polling loops — if you choose the right granularity.

Before observers, production JavaScript was full of scroll handlers asking every element where it was sixty times per second. The layout engine answered, the main thread thrashed, and battery life became a rounding error. Observers invert the flow: tell the browser what you care about, and it batches notifications when the platform knows something changed.

Visibility without scroll math

const observer = new IntersectionObserver(entries => {
  for (const entry of entries) {
    if (entry.isIntersecting) hydrateCard(entry.target);
  }
}, { rootMargin: "300px" });

document.querySelectorAll(".lazy-card").forEach(el => observer.observe(el));

IntersectionObserver is for lazy loading, infinite scroll sentinels, and analytics visibility. rootMargin lets you preload before the user reaches content. It is not a security boundary or a perfect ad-impression oracle: occlusion, browser heuristics, and edge cases exist.

Resize the component, not the window

ResizeObserver answers the component-level question window resize never could: did this box change because a sidebar collapsed, a font loaded, or a container query shifted layout? Use it for canvases, charts, editors, and virtualized layouts. Inside the callback, batch DOM writes and avoid loops where changing size triggers another observed size change.

MutationObserver is a scalpel

MutationObserver watches DOM trees for added nodes or attribute changes. It is invaluable when integrating with widgets you do not control. It is also easy to turn into a recursive mutation machine: observer changes DOM, observer fires, callback changes DOM again. Keep mutation handlers narrow, disconnect when possible, and do not use it as a substitute for owning your rendering pipeline.

PerformanceObserver closes the loop

PerformanceObserver surfaces real timing entries — LCP, long tasks, resource timing, navigation — without scraping performance manually at random moments.

new PerformanceObserver(list => {
  for (const entry of list.getEntries()) reportLongTask(entry.duration);
}).observe({ type: "longtask", buffered: true });

This is how you correlate API design decisions with user experience. A worker migration that removes 120 ms long tasks will show up here even when synthetic benchmark scores barely move.

Field notes

  • Every observer has disconnect(); use it when views unmount. Observers are easy leaks in single-page apps.
  • Observer callbacks are batched and asynchronous. Do not assume frame-perfect timing.
  • getBoundingClientRect in a scroll listener forces layout; observer-based visibility almost always wins.
  • Observer support is broad, but behavior differences remain. Verify on Safari mobile before shipping layout-critical assumptions.

Was this lesson helpful?

Sign in to rate