Debounce and throttle
Debounce
Flow.debounce reduces a series of function calls that happen within a duration to a single call. It allows you to ignore all events until there is a break in the flow.
For example, only respond to resize event 500ms after the last resize event. If no event has happened before, it will still wait 500ms.
Note in the demo that lots of events (ie. movement) produce no debounced result until the events stop. If you want to have a continual stream of events, albeit at a slower rate, consider throttle.
Throttle
Flow.throttle reduces a fast interval of function calls to a maximum rate. It allows you to ignore an event if it happened too soon after the previous event.
This is useful when processing event or stream data (eg user input, camera or audio feeds). In some scenarios the events come in to your code faster than you can process them. This results in a choked computer (laggy, unresponsive) and a backlogged response. But with throttle and an appropriate intervalMs
, you might avoid this.
In this scenario, the event handler will run at maximum rate of 500ms.
throttle
handles an initial event (when nothing has happened before), and continue to handle events, albeit at a reduced rate. debounce
, in comparison, only handles the first event after timeoutMs
has elapsed. And if a new event comes in the meantime, the timer is reset again. It’s only when there is a break in the events that debounce
will run.
It’s also possible to use throttle
and debounce
without events being involved.