New:Microsoft Teams Notifications Are Now Available in Socket.Learn more
Get Started

devkits-debounce

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

devkits-debounce

Debounce and throttle functions — zero dependencies

latest
Source
npmnpm
Version
1.0.0
Version published
Weekly downloads
0
-100%
Maintainers
1
Weekly downloads
 
Created
Source

devkits-debounce

Debounce and throttle functions. Zero dependencies, lightweight utility for rate limiting function calls.

Install

npm install -g devkits-debounce

npm version

Usage

CLI

# Run demo
debounce demo

# Run tests
debounce test

Programmatic API

const { debounce, throttle } = require('devkits-debounce');

// Debounce search input
const search = debounce((query) => {
  fetchResults(query);
}, 300);

// Throttle scroll handler
const onScroll = throttle(() => {
  updatePosition();
}, 100);

API

debounce(fn, wait, options)

Create a debounced version of a function.

ParamTypeDescription
fnFunctionFunction to debounce
waitnumberWait time in milliseconds (default: 0)
options.leadingbooleanCall on leading edge (default: false)
options.trailingbooleanCall on trailing edge (default: true)
options.maxWaitnumberMaximum wait before forcing execution
ReturnsFunctionDebounced function

Methods:

  • .cancel() — Cancel pending execution
  • .flush() — Execute immediately if pending

throttle(fn, wait, options)

Create a throttled version of a function.

ParamTypeDescription
fnFunctionFunction to throttle
waitnumberWait time in milliseconds (default: 0)
options.leadingbooleanCall on leading edge (default: true)
options.trailingbooleanCall on trailing edge (default: true)
ReturnsFunctionThrottled function

Methods:

  • .cancel() — Cancel pending execution
  • .flush() — Execute immediately if pending

Use Cases

Debounce

  • Search inputs — Wait until user stops typing
  • Window resize — Handle resize events efficiently
  • Form auto-save — Save after user pauses
  • API calls — Rate limit rapid requests
// Search with debounce
const searchInput = document.getElementById('search');
const search = debounce((query) => {
  api.search(query).then(renderResults);
}, 300);

searchInput.addEventListener('input', (e) => {
  search(e.target.value);
});

Throttle

  • Scroll handlers — Limit scroll event frequency
  • Button clicks — Prevent double-submit
  • Drag operations — Smooth drag updates
  • Rate limiting — Control API call frequency
// Scroll with throttle
const onScroll = throttle(() => {
  const position = window.scrollY;
  updateProgressBar(position);
}, 100);

window.addEventListener('scroll', onScroll);

Examples

// Debounce with leading edge
const debounced = debounce(() => {
  console.log('Executed!');
}, 300, { leading: true, trailing: true });

// Throttle with only trailing edge
const throttled = throttle(() => {
  console.log('Throttled!');
}, 1000, { leading: false, trailing: true });

// Cancel pending execution
const task = debounce(expensiveOperation, 1000);
task();
task.cancel(); // Won't execute

// Force immediate execution
const task2 = debounce(saveData, 5000);
task2();
task2.flush(); // Execute now

Features

  • Zero dependencies — Pure JavaScript
  • Full-featured — leading/trailing options, maxWait, cancel, flush
  • Lightweight — ~3KB minified
  • CLI + library — Command line and programmatic use
  • Well-tested — Edge cases covered
  • lodash.debounce — Lodash's debounce (larger bundle)
  • lodash.throttle — Lodash's throttle

See Also

Support

Open Collective: Open Collective - DevKits

Crypto: ETH/USDC 0xDea4a6A20fCB44467e45Ef378972F54B22dC59db

License

MIT — DevKits Team

Keywords

devkits

FAQs

Package last updated on 12 Mar 2026

Related posts