Debouncing vs Throttling in Frontend Development

🚀 Day 3/30 – Frontend Interview Series 📌 Topic: Debouncing vs Throttling If you’ve ever worked on search inputs, scroll events, or button clicks — you’ve probably faced performance issues. 👉 That’s where Debouncing and Throttling come into play. 🔹 What is Debouncing? Debouncing ensures that a function is called only after a certain delay once the user stops triggering the event. 📌 Example: Search input field (API call after user stops typing) function debounce(fn, delay) { let timer; return function (...args) { clearTimeout(timer); timer = setTimeout(() => { fn.apply(this, args); }, delay); }; } 👉 Use when: ✔ API calls ✔ Input validation ✔ Auto-save 🔹 What is Throttling? Throttling ensures that a function is called at most once in a given time interval, no matter how many times the event is triggered. 📌 Example: Scroll or resize events function throttle(fn, limit) { let flag = true; return function (...args) { if (flag) { fn.apply(this, args); flag = false; setTimeout(() => { flag = true; }, limit); } }; } 👉 Use when: ✔ Scroll events ✔ Window resize ✔ Button spam prevention #JavaScript #FrontendDeveloper #ReactJS #WebDevelopment #InterviewPrep #CodingInterview #100DaysOfCode

To view or add a comment, sign in

Explore content categories