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

smart-debounce

v1.0.4

Published

A smart, tiny, zero-dependency debounce function for sync and async functions in TypeScript.

Readme

Smart Debounce

Smart Debounce is a lightweight, type-safe, zero-dependency utility for debouncing function calls in JavaScript/TypeScript. It offers advanced features such as async support, cancellation, rate-limiting, and more.

Features

  • Type-safe: Written in TypeScript with full type safety.
  • Zero dependencies: No third-party dependencies.
  • Async support: Debounced functions work seamlessly with promises.
  • Rate-limiting: Support for controlling function execution frequency.
  • Cancellation and Flush: Manual control to cancel or flush the debounce queue.
  • Leading and Trailing edge support: Control whether the debounced function runs at the beginning or end of the debounce interval.
  • Max Calls per Wait Window: Option to limit the number of calls within a single debounce window.

Installation

You can install the package via npm:

npm install smart-debounce

Or using yarn:

yarn add smart-debounce

Usage

Basic Example

import { smartDebounce } from 'smart-debounce';

const logMessage = (message: string) => {
  console.log(`Logged: ${message}`);
};

const debouncedLog = smartDebounce(logMessage, { wait: 1000 });

debouncedLog('Message 1'); // This will be debounced
debouncedLog('Message 2'); // This will override the previous call
debouncedLog('Message 3'); // This will trigger the log after 1 second

Advanced Example

import { smartDebounce } from 'smart-debounce';

const fetchUser = async (query: string) => {
  console.log(`Fetching user for: ${query}`);
  // Simulate an API request
  return new Promise(resolve => setTimeout(() => resolve({ query, user: 'John Doe' }), 2000));
};

const debouncedFetchUser = smartDebounce(fetchUser, {
  wait: 1000,
  maxWait: 3000,
  leading: false,
  trailing: true,
});

debouncedFetchUser('user 1');
debouncedFetchUser('user 2');
debouncedFetchUser('user 3');

// This will log the last request after 1 second

Manual Control

You can cancel or flush the debounce queue manually:

debouncedLog.cancel();  // Cancels the debounce
debouncedLog.flush();   // Executes the debounced function immediately

React Example

import React, { useState } from 'react';
import { smartDebounce } from 'smart-debounce';

const SearchComponent = () => {
  const [query, setQuery] = useState('');

  const handleChange = smartDebounce((event: React.ChangeEvent<HTMLInputElement>) => {
    console.log('Searching for:', event.target.value);
    // Perform search or API call here
  }, { wait: 500 });

  return (
    <div>
      <input
        type="text"
        value={query}
        onChange={(e) => {
          setQuery(e.target.value);
          handleChange(e);
        }}
        placeholder="Search..."
      />
    </div>
  );
};

export default SearchComponent;

Angular Example

import { Component } from '@angular/core';
import { smartDebounce } from 'smart-debounce';

@Component({
  selector: 'app-search',
  template: `
    <input [(ngModel)]="query" (ngModelChange)="onSearch($event)" placeholder="Search..."/>
  `,
})
export class SearchComponent {
  query: string = '';

  onSearch = smartDebounce((query: string) => {
    console.log('Searching for:', query);
    // Perform search or API call here
  }, { wait: 500 });
}

API

smartDebounce(fn, options)

  • fn: The function to debounce (required).
  • options: Configuration options (optional).
    • wait: Delay in milliseconds before calling the function (default: 300).
    • maxWait: Maximum time before the function is called (default: undefined).
    • leading: If true, the function is invoked at the leading edge of the wait interval (default: false).
    • trailing: If true, the function is invoked at the trailing edge of the wait interval (default: true).
    • maxCallsPerWaitWindow: Maximum number of calls allowed within a debounce window (default: undefined).

cancel()

  • Cancels the current debounce and clears any pending executions.

flush()

  • Immediately invokes the debounced function and clears any pending executions.

reset()

  • Resets the debounce state, clearing any timeouts and pending calls.

License

MIT License