Three tools, one API
debounce waits for quiet, throttle keeps a steady rate and rafThrottle runs once per animation frame.
Debounce input, throttle work and run layout code once per frame with one small, dependency-free package.
Install it, try the live counter, then read the full reference.
npm install @samline/debounce764 B gzipZero dependenciesMIT licensed
ONE SMALL SCRIPT. RIGHT AT HOME IN YOUR STACK.
debounce waits for quiet, throttle keeps a steady rate and rafThrottle runs once per animation frame.
Drop a pending call when a dialog closes, run it now on submit, or check whether one is scheduled.
leading, trailing and maxWait follow the well-known lodash semantics, with this and the last result forwarded.
Invalid waits become 0 and rafThrottle falls back to a timer without requestAnimationFrame, so SSR never throws.
React hooks, Vue composables and a Svelte store keep the latest callback and cancel on unmount.
Every entry point has a gzip budget checked in CI. The core is under 1 KB.
Import only what you need: the root entry tree-shakes, adapters are subpaths.
Package
debounce, throttle and rafThrottle as ESM or CommonJS.
React
useDebouncedCallback, useThrottledCallback and useDebouncedValue.
Vue
useDebouncedCallback, useThrottledCallback and useDebouncedRef.
HTML / CDN
A classic script for pages without a build step.
Svelte
createDebouncedStore with flush and cancel.
Types
Typed options and returned functions, no CSS required.
Debounce a search field so the request runs once the user stops typing.
Read the installation guideInstall
npm
npm install @samline/debouncepnpm
pnpm add @samline/debounceyarn
yarn add @samline/debouncebun
bun add @samline/debounceHTML / CDN
<script src="https://cdn.jsdelivr.net/npm/@samline/debounce@1.0.2/dist/browser/debounce.global.js" defer></script>Add markup
A field and a status line. No special attributes are required.
<label for="search">Search</label>
<input id="search" type="search" autocomplete="off" />
<p id="status">Type to search.</p>Debounce the handler
import { debounce } from '@samline/debounce'
const input = document.querySelector('#search')
const status = document.querySelector('#status')
const search = debounce(query => {
status.textContent = query ? `Searching for “${query}”…` : 'Type to search.'
}, 300)
input.addEventListener('input', () => search(input.value))Continue with leading, trailing, maxWait and the returned controls.
Explore the optionsThree common jobs, each with the right tool.
Run the pending search immediately when the form is submitted.
Read the APIform.addEventListener('submit', () => search.flush())Save at most once per second while the user keeps typing.
Read the optionsconst save = throttle(() => persist(editor.value), 1000)
editor.addEventListener('input', save)Coalesce scroll events into one layout read per animation frame.
Read the APIconst update = rafThrottle(() => bar.style.width = progress() + '%')
window.addEventListener('scroll', update, { passive: true })Try the examples or follow the first steps.
Guides, the complete API and release notes, in one place.
Read the documentation