Event Listeners help us in binding functions which are called when that event is emitted, like scrolling or resizing.

But what if the event is called multiple times in a very short span of time? If the function called by the listener is intensive, it can severely impact performance.

Debouncing and Throttling are really useful under such scenarios.

Throttling

Throttling essentially let’s you execute a function only once in a given period of time.
If the time duration given is 500 milliseconds, no matter how many times the event is emitted in those 500 milliseconds, the event will only execute once. Assuming an event is fired continuously for 3 seconds, the above example would let the function fire only 6 times.

#debouncing #throttling #javascript

Debouncing and Throttling in JavaScript
1.85 GEEK