Implementing Debounce Function in JavaScript for Improved Performance

🚀 Day 2/100 – Understanding Debounce (With Execution) Today I implemented a Debounce function in JavaScript and tested it with real execution. 🧠 Problem: Create a debounce function that delays execution until the user stops triggering it for a specified time. 📌 Common Use Case: Search input field to prevent API calls on every keystroke. ✅ Solution: function debounce(func, delay) { let timer; return function (...args) { clearTimeout(timer); timer = setTimeout(() => { func.apply(this, args); }, delay); }; } // Example function handleSearch(query) { console.log("Searching for:", query); } const debouncedSearch = debounce(handleSearch, 1000); // Simulating rapid calls debouncedSearch("R"); debouncedSearch("Re"); debouncedSearch("Rea"); debouncedSearch("React"); ⏳ After 1 second, output will be: Searching for: React Earlier calls are cancelled automatically. 💡 Key Learning: clearTimeout prevents multiple executions Improves performance Reduces unnecessary API calls Small improvements like this make a big difference in real applications 🚀 I’m currently open to Frontend Developer opportunities (React / Next.js) and available for immediate joining. 📩 Email: bantykumar13365@gmail.com 📱 Mobile: 7417401815 If you're hiring or know someone who is, feel free to connect or reach out. #OpenToWork #FrontendDeveloper #JavaScript #ReactJS #NextJS #ImmediateJoiner #100DaysOfCode

To view or add a comment, sign in

Explore content categories