fast-debounce
v0.9.5
Published
A debounce() implementation faster than setTimeout
Downloads
2
Readme
fast-debounce
A fast debounce()
implementation. Fires faster than setTimeout(..., 0)
.
Example
let deb = debounce((...args) => console.log(...args));
deb(1);
deb(2);
deb(3);
setTimeout(() => {
deb(4);
},0);
setTimeout(() => {
deb(6);
},2);
setTimeout(() => {
deb(5);
},0);
setTimeout(() => {
deb(7);
},15);
Will print: 3,4,5,6,7
Whereas this implementation:
function debounce(fn) {
let t;
return (...args) => {
clearTimeout(t);
t = setTimeout(() => {
fn(...args);
},0);
}
}
Will print:
3,6,7.
i.e., they block block successive function invocations, but the latter depends on a low-resolution timer that will only fire every ~4-10ms instead of on the next tick.