
Security News
Vite+ Joins the Push to Consolidate JavaScript Tooling
Evan You announces Vite+, a commercial, Rust-powered toolchain built on the Vite ecosystem to unify JavaScript development and fund open source.
@avatijs/debounce
Advanced tools
TypeScript Debounce is an elegant, robust debounce utility that brings the power of controlled function execution to your TypeScript applications. It provides a clean, type-safe API for managing function call rates, preventing resource overuse, and improving application performance.
While there are many debounce implementations available, this library stands out by offering:
Debouncing is crucial in many common development scenarios:
Search Input Handling
// Without debounce - Makes API call on every keystroke
searchInput.addEventListener('input', async (e) => {
const results = await searchAPI(e.target.value); // 🔴 Excessive API calls
});
// With debounce - Waits for user to stop typing
const debouncedSearch = debounce(async (value: string) => {
const results = await searchAPI(value);
}, { wait: 300 }); // ✅ Single API call after typing stops
Window Resize Handling
// Without debounce - Recalculates layout on every resize event
window.addEventListener('resize', () => {
recalculateLayout(); // 🔴 Performance bottleneck
});
// With debounce - Controlled recalculation
const debouncedResize = debounce(() => {
recalculateLayout();
}, { wait: 150 }); // ✅ Smooth performance
Form Validation
// Without debounce - Validates on every change
input.addEventListener('input', async (e) => {
await validateField(e.target.value); // 🔴 Excessive validation
});
// With debounce - Validates after user stops typing
const debouncedValidate = debounce(async (value: string) => {
await validateField(value);
}, { wait: 400 }); // ✅ Efficient validation
Real-time Saving
// Without debounce - Saves on every change
editor.on('change', async (content) => {
await saveContent(content); // 🔴 Too many save operations
});
// With debounce - Intelligently batches saves
const debouncedSave = debounce(async (content: string) => {
await saveContent(content);
}, { wait: 1000 }); // ✅ Optimized saving
npm install typescript-debounce
# or
yarn add typescript-debounce
# or
pnpm add typescript-debounce
import { debounce } from '@avatijs/debounce';
// Create a debounced function
const debouncedFn = debounce(async (value: string) => {
const result = await api.search(value);
updateUI(result);
}, {
wait: 300, // Wait 300ms after last call
leading: false, // Don't execute on leading edge
trailing: true, // Execute on trailing edge
maxWait: 1000, // Maximum time to wait
debug: true, // Enable debug logging
onError: console.error // Error handling
});
// Use the debounced function
try {
await debouncedFn('search term');
} catch (error) {
handleError(error);
}
Checkout the Demo to see TypeScript Debounce in action.
Please see CHANGELOG for more information what has changed recently.
I welcome contributions from developers of all experience levels. If you have an idea, found a bug, or want to improve something, I encourage you to get involved!
Avati is open-source and distributed under the MIT License.
FAQs
Debounce package part of Avati project
We found that @avatijs/debounce demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Evan You announces Vite+, a commercial, Rust-powered toolchain built on the Vite ecosystem to unify JavaScript development and fund open source.
Security News
Ruby Central’s incident report on the RubyGems.org access dispute sparks backlash from former maintainers and renewed debate over project governance.
Research
/Security News
Socket researchers uncover how threat actors weaponize Discord across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.