JavaScript, inside out · Chapter 47 of 51
Debounce and throttle
By the end of this chapter you can write debounce and throttle from scratch, explain the difference, and give a debounced function a cancel method. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: The interview
- 4 tasks
- JavaScript
- In the all labs plan
The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.
Why you need this
Round two of Dev's interview. He types "masala" into the Tapri menu search, and the network tab fills with six requests, one per keystroke. "Our server pays for every one of those," he says. "And the rider tracker redraws the map on every GPS tick. Fix both, and no lodash."
You already have the pieces: setTimeout and closures from chapter 21, this and apply from chapters 24 and 25. Calling the search on every keystroke is exactly what Dev is complaining about, and a plain setTimeout around it only delays all six requests instead of dropping five.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1 · interview question
Interview: debounce, run once after the typing stops
Write debounce(fn, wait). It returns a new function. Calling that function does not run fn straight away: fn runs once, wait ms after the last call of a burst, and every new call starts the wait again (299 ms after the last call nothing has run yet, at 300 ms it has, with a wait of 300).
fn gets every argument of the last call, and the this the debounced function was called with (for example when it is an object's method). A later burst runs fn again. Each debounced function has its own timer, so two of them never cancel each other.
With no third argument, or an empty options object, debounce works this way. No libraries: setTimeout and clearTimeout are all you need.
- Task 2 · interview question
Interview: debounce with leading, run at the start of a burst
Add the option debounce(fn, wait, { leading: true }). Now the first call of a burst runs fn straight away, with that call's arguments and this.
Further calls inside the burst do not run fn, and each one starts the quiet period of wait ms again. Once wait ms pass with no call, the burst is over and nothing runs at its end. The next call after that runs fn straight away again.
Without the option, debounce must still work as in task 1.
- Task 3 · interview question
Interview: throttle, at most once per window
Write throttle(fn, wait). It returns a new function. When the throttle is idle, a call runs fn straight away and opens a window of wait ms.
Calls inside the window do not run fn, but the last one is remembered (its arguments and its this). When the window ends, if a call was remembered, fn runs with it and a new window of wait ms opens. If none was, the throttle goes idle, and the next call runs straight away.
fn always gets every argument and the this of the call it runs for, both for a call that runs at once and for a remembered one.
- Task 4 · interview question
Interview: cancel, call off a debounced function
Give every function debounce returns a method cancel(). It drops the call that is waiting, so fn does not run for it. Calling cancel when nothing is waiting is harmless.
After a cancel the function keeps working: the next call is debounced as usual. Cancelling one debounced function leaves any other alone.
For a leading debounce, cancel ends the burst, so the very next call runs fn straight away.
What you will learn
- Calling off a timer: clearTimeout and the id that setTimeout gives back.
- A wrapper that passes everything through: the caller's this and every argument, also from inside a timer.
- Debounce and throttle: wait for a pause, or run at a steady rate.
- A function with a method: functions are objects, so they can carry a cancel.
More in The interview
Build "Debounce and throttle" yourself
Tapri, a chai brand going online, needs its menu, orders and kitchen in code. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of JavaScript, inside out are free. This one comes with the all labs plan.