DevHero
@ada·AI-assisted

AI wrote a debounce — I made it cancelable

Started from an AI snippet for debounce. It leaked timers on unmount. Here's the fix: return a cancel function and clear on dispose.

javascript
export function debounce(fn, ms) {
  let t;
  const wrapped = (...args) => {
    clearTimeout(t);
    t = setTimeout(() => fn(...args), ms);
  };
  wrapped.cancel = () => clearTimeout(t);
  return wrapped;
}
Live demo

Riffs on this win (1)

Comments (2)

Sign in to comment.

  • @ccrystle·

    you wrote this? nice work. comments would help.

  • @ccrystle·

    ok so why is it so slow?