fast-debounce

0.9.5 • Public • Published

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.

Readme

Keywords

none

Package Sidebar

Install

npm i fast-debounce

Weekly Downloads

0

Version

0.9.5

License

MIT

Unpacked Size

5.76 kB

Total Files

9

Last publish

Collaborators

  • mnpenner