Debounce vs Throttle: An AI Agent Wrote Both, and One Ran 5 Times While One Ran Once
Debounce and throttle get treated as interchangeable synonyms for "slow this function down." They are not. I asked an AI agent to write both in Node.js and I ran them for real on a simulated clock. The full run is on vid
Debounce and throttle get treated as interchangeable synonyms for "slow this function down." They are not. I asked an AI agent to write both in Node.js and I ran them for real on a simulated clock. The full run is on video: https://youtu.be/GmsIIMlFATU
The test
50 events arrive 20 ms apart, from t = 0 to t = 980 ms. The wait period is 200 ms. Think of it as a search box being typed into, or a scroll handler firing continuously for about a second.
Throttle: 5 calls
Throttle limits how often the function can run: at most once per 200 ms window. The calls landed at 0, 200, 400, 600 and 800 ms: 5 calls, evenly spaced, for the whole burst. The function keeps running at a steady drip while events keep coming.
Debounce: 1 call
Debounce waits for the events to stop. Every new event resets its 200 ms timer, so as long as events keep arriving 20 ms apart, the function never fires. It ran exactly once, at 1180 ms: 200 ms after the last event.
Same 50 events, same 200 ms wait: 5 calls versus 1. The rule of thumb from this test: debounce waits for events to stop, throttle limits how often they run. A resize handler that recalculates layout mid-drag wants throttle. A search box that should only query when you pause typing wants debounce.
What I did not test
Leading and trailing edge options, real timers, and real browser events are all still open. The clock is simulated, not wall time. In production, reach for a battle-tested utility. But if you have ever reached for one and meant the other, this run shows the difference in the starkest terms.
Watch the run
The code, the terminal output, and every call timestamp are in the video: https://youtu.be/GmsIIMlFATU
Subscribe to Ship Stack AI on YouTube and tell me in the comments what to build and break next.
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.