Under Construction
Backend••10 min•105 views••

How are throttling and debouncing different?

Minh Khoa

Minh Khoa

Author

image.pngWhen a player drags a slider or types into a search box, each change can generate an event. If every event sends a request or runs an expensive computation, the system will do more work than necessary.

Throttling limits execution frequency. Debouncing waits for the action to pause before executing. Which approach you choose depends on whether the user needs to see the result immediately while interacting, or only after the interaction has stabilized.

Throttling — keep updating while the player is interacting

Suppose a multiplayer lobby lets players drag a slider to change shirt color. The color on their own character updates immediately, while the game sends the new color so others in the room can see it.

If the slider produces dozens of events per second, you can throttle the data-sending function separately with an interval of 200 ms between two sends. The player can still keep dragging the slider continuously, but others receive updates about 5 times per second when the data is changing continuously. This is a way to keep feedback responsive during interaction while limiting the number of times processing occurs. MDN — Throttle

Debouncing — process after the player stops changing the data

With a search box in the inventory, the player wants to type sword. During typing, the input field successively contains s, sw, swo, swor, then sword.

If you debounce the search function 300 ms, each new character will restart the wait time. When the player stops typing for 300 ms, the game will search based on the latest content, which is sword. If they pause for a long time at sw, the search may still run with sw. Debounce determines when to process based on the gaps between events. MDN — Debounce

Comparison on the same event sequence

Suppose there are events at the timestamps 0, 100, 200, 300, 400, 500, and 600 ms.

Processing methodWhen the function runs
Handle every event0, 100, 200, 300, 400, 500, 600 ms
Throttle 200 ms, run immediately the first time0, 200, 400, 600 ms
Debounce 300 ms, wait after the last event900 ms

In the throttle row, events that arrive too soon after the most recent execution are ignored. In the debounce row, the event at 600 ms pushes the execution time to 900 ms. If there is another event at 800 ms, that time is pushed further to 1100 ms.

When implementing a slider, pay attention to the final value. If the player releases the mouse between two sends, send the final state on the drag-end event or use a throttle with a trailing call, often called trailing. The example in the figure uses leading-only to clearly illustrate the frequency-limiting mechanism. Libraries can support different configurations for the first and last call. Lodash — Throttle

NeedSuitable approach
Need to update while the data continues changingThrottle
Only need to process after the data has been stable for a period of timeDebounce

The 200 ms and 300 ms values in the article are for illustration. When applying them, adjust according to the processing cost and the delay that the interaction can tolerate.