Search-as-you-type can fire an input handler 20 times in 800 ms. A scroll listener can fire once per frame. Neither problem is fixed by making the handler faster; it is fixed by deciding how often the handler is allowed to run.
The mental model
Debounce collapses a burst into one call: after the last call, wait wait ms, then run once.
Throttle guarantees spacing: run at most once per wait ms while calls keep coming, then run once more with the final state.
Debounce answers "did the user stop?" Throttle answers "how often do we allow work?" If you cannot say which question your feature asks, you are about to pick the wrong one. Both helpers below are trailing by default; add a leading branch only when the first call must be instant.
The implementation
No dependencies, no classes, no this games:
const debounce = ( fn , wait ) => { let t ; return ( . . . a ) => { clearTimeout ( t ) ; t = setTimeout ( ( ) => fn ( . . .…