Skip to content

A lightweight, dependency-free debounce utility for JavaScript functions. Supports both leading and trailing edge execution. Perfect for input handlers, resize events, and performance optimization in modern web apps.

License

Notifications You must be signed in to change notification settings

mrnko/lite-debounce-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LiteDebounce

mrnko - lite-debounce-js stars - lite-debounce-js forks - lite-debounce-js

A lightweight, dependency-free debounce utility for JavaScript functions. Supports both leading and trailing edge execution. Perfect for input handlers, resize events, and performance optimization in modern web apps.

npm version View DemoView on GitHub


Features

  • Zero dependencies
  • ESM build
  • Leading/trailing edge debounce
  • Context (this) support
  • Tiny & fast

Installation

npm install lite-debounce-js

Or simply copy the file from dist/ into your project.


Usage

Import

// ESM (from npm)
import { LiteDebounce } from 'lite-debounce-js';
// Or import from local build:
import { LiteDebounce } from './dist/lite-debounce.min.js';

Basic trailing debounce (default)

const debouncer = new LiteDebounce(() => {
  console.log('Debounced!');
}, 300); // 300ms delay

window.addEventListener('resize', debouncer.fnDebounced);

Leading debounce

const debouncer = new LiteDebounce(() => {
  console.log('Leading call!');
}, 500, { leading: true });

const debouncedFn = debouncer.fnDebounced;
debouncedFn(); // will be called immediately

With arguments and context

const obj = {
  value: 42,
  log() {
    console.log(this.value);
  }
};
const debouncer = new LiteDebounce(obj.log, 200);
const debouncedFn = debouncer.fnDebounced.bind(obj);
debouncedFn(); // logs: 42

API

new LiteDebounce(fn, delay = 300, options = {})

  • fn — function to debounce
  • delay — debounce delay in ms (default: 300)
  • options.leading — if true, call on the leading edge (default: false)

Instance property:

  • .fnDebounced — debounced function, use it in event listeners or anywhere

Demo

Open demo/index.html in your browser (with a local server) or run the dev server:

npm run dev

Testing

npm test

Tests are written with Vitest.

About

A lightweight, dependency-free debounce utility for JavaScript functions. Supports both leading and trailing edge execution. Perfect for input handlers, resize events, and performance optimization in modern web apps.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published