npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@chaisser/debounce-throttle

v1.0.4

Published

Simple debounce and throttle functions with cancel support

Readme

@chaisser/debounce-throttle

Simple debounce and throttle functions with cancel support

Lightweight, zero-dependency rate limiting utilities. Debounce delays execution until after a quiet period; throttle limits execution to at most once per interval. Both support leading/trailing edge invocation and cancel/flush controls.


Installation

npm install @chaisser/debounce-throttle
# or
yarn add @chaisser/debounce-throttle
# or
pnpm add @chaisser/debounce-throttle

Quick Start

import { debounce, throttle, delay } from '@chaisser/debounce-throttle';

// Debounce: fire after 300ms of silence
const search = debounce((query: string) => {
  console.log('Searching:', query);
}, 300);

search('he');
search('hel');
search('hell');
search('hello'); // only this fires after 300ms

// Throttle: fire at most once per 1000ms
const onScroll = throttle(() => {
  console.log('Scroll position:', window.scrollY);
}, 1000);

window.addEventListener('scroll', onScroll);

API Reference

debounce(func, wait, options?)

Creates a debounced function that delays invoking func until after wait milliseconds have elapsed since the last call.

| Param | Type | Description | |-------|------|-------------| | func | (...args) => any | Function to debounce | | wait | number | Delay in milliseconds | | options.leading | boolean | Invoke on the leading edge (default: false) | | options.trailing | boolean | Invoke on the trailing edge (default: true) |

Returns a DebouncedFunction with .cancel() and .flush() methods.

throttle(func, wait, options?)

Creates a throttled function that invokes func at most once per wait milliseconds.

| Param | Type | Description | |-------|------|-------------| | func | (...args) => any | Function to throttle | | wait | number | Interval in milliseconds | | options.leading | boolean | Invoke on the leading edge (default: true) | | options.trailing | boolean | Invoke on the trailing edge (default: true) |

Returns a ThrottledFunction with .cancel() and .flush() methods.

delay(wait)

Returns a Promise<void> that resolves after wait milliseconds.

Cancelable Methods

Both debounce and throttle return functions with:

| Method | Description | |--------|-------------| | .cancel() | Cancel pending execution and reset state | | .flush() | Immediately invoke pending function and clear timer |


Examples

Cancel a Debounced Call

import { debounce } from '@chaisser/debounce-throttle';

const save = debounce((data: string) => {
  console.log('Saved:', data);
}, 1000);

save('draft');

// User navigated away before save fired
save.cancel();

Flush Immediately

import { debounce } from '@chaisser/debounce-throttle';

const log = debounce((msg: string) => {
  console.log(msg);
}, 2000);

log('hello');

// Force immediate execution instead of waiting
log.flush(); // logs 'hello' now

Leading Edge Only

import { debounce, throttle } from '@chaisser/debounce-throttle';

// Fire immediately on first call, ignore subsequent calls within 500ms
const onFirstClick = debounce(() => {
  console.log('Action triggered');
}, 500, { leading: true, trailing: false });

// Throttle: fire immediately, then silence for 1s
const onSave = throttle(() => {
  console.log('Saving...');
}, 1000, { leading: true, trailing: false });

Trailing Edge Only (Default Throttle)

import { throttle } from '@chaisser/debounce-throttle';

// Wait until the interval passes, then fire with the last args
const onMouseMove = throttle((x: number, y: number) => {
  console.log(`Mouse: ${x}, ${y}`);
}, 100, { leading: false, trailing: true });

Promise-based Delay

import { delay } from '@chaisser/debounce-throttle';

async function retryWithBackoff(fn: () => Promise<void>, attempts: number) {
  for (let i = 0; i < attempts; i++) {
    try {
      await fn();
      return;
    } catch {
      await delay(1000 * Math.pow(2, i));
    }
  }
}

License

MIT