On this page
Observers & Web Workers
Prev: Storage & State | Up: apis | Next: rendering
Rel: Reflow, Repaint & Performance, Event Loop & Async Programming
Ref: MDN Web Docs — Intersection Observer API
Tags: #public #web #platform #apis #observers
Contents
Browser Observer APIs
Replace expensive scroll and resize listeners with purpose-built asynchronous observers:
IntersectionObserver: Detects when elements enter or leave the viewport (lazy loading, infinite scroll, reveal animations).ResizeObserver: Tracks an element's content/border box size changes.MutationObserver: Watches DOM attribute or child-list mutations.
const observer = new IntersectionObserver(
(entries) => {
for (const entry of entries) {
if (entry.isIntersecting) {
entry.target.classList.add("is-visible");
observer.unobserve(entry.target);
}
}
},
{ rootMargin: "100px" }
);
document.querySelectorAll(".reveal").forEach((el) => observer.observe(el));
Off-Main-Thread Compute (Worker)
Move heavy CPU work off the UI thread with new Worker("worker.js", { type: "module" }), communicating via postMessage() (using structured cloning or zero-copy ArrayBuffer transfers).
Linked from
Related notes
- tuts/web/apis Linked here; links to this page; shared tags: #apis, #platform, #web
- tuts/web/apis/storage_and_state Linked here; links to this page; shared tags: #apis, #platform, #web
- tuts/web/rendering/reflow_repaint_and_performance Linked here; links to this page; shared tags: #platform, #web
- tuts/web/rendering Linked here; shared tags: #platform, #web
- tuts/javascript/tutorials/event_loop_and_async_programming Linked here; shared tags: #web
- tuts/web/apis/fetch_and_networking shared tags: #apis, #platform, #web
- tuts/python3/tutorials/popular_application_domains/web_development_and_apis shared tags: #apis, #web
- tuts/typescript/tutorials/type_safe_api_clients_and_validation shared tags: #apis, #web
- tuts/web shared tags: #platform, #web
- tuts/web/dom shared tags: #platform, #web