{"success":true,"name":"debounce-and-throttle-high-frequency-events-in-javascript","description":"JS code reacts to typing, scrolling, resizing, or rapid repeated events.","category":"ai-brimind","path":"ai-brimind/debounce-and-throttle-high-frequency-events-in-javascript/SKILL.md","file":"SKILL.md","content":"---\nname: debounce-and-throttle-high-frequency-events-in-javascript\ndescription: \"JS code reacts to typing, scrolling, resizing, or rapid repeated events.\"\n---\n\n# Debounce and Throttle High-Frequency Events in JavaScript\n\n## When to use\nProject is JavaScript, and an event that fires many times per second (input, scroll, resize, mousemove) triggers expensive work like fetches, DOM updates, or calculations.\n\n## Example request\nAPI call on every keystroke → 15 requests for one word, most instantly stale — debounce: act only after the user pauses. Heavy layout code on every scroll pixel → page stutters — throttle: act at most every N ms. Resize handler rebuilding a chart 60 times per second → browser freezes during resize — debounce the rebuild to the end of resizing.\n\n## Instructions\nAct like a developer who counts how many times a handler fires before writing what it does. When wiring a handler to a high-frequency event: (1) measure or reason about the firing rate — input and scroll can fire dozens of times per second; (2) classify the work: if only the FINAL state matters (search query, resize result), DEBOUNCE — wait until events stop for N ms, then run once; if intermediate updates matter (scroll position UI), THROTTLE — run at most once per interval; (3) check for platform alternatives first: CSS can often replace scroll listeners, IntersectionObserver replaces visibility polling, ResizeObserver replaces window resize hacks — built-ins beat custom timers. Keep the raw handler itself trivially cheap; the expensive function is what gets wrapped. Only proceed once you can state the expected call rate before and after ('was ~30/sec, now at most 1 per 300ms') and why that specific strategy (debounce vs throttle vs built-in observer) matches how the result is used.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #1012\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}