Real problem:
Search inputs trigger too many backend calls.
✔ Debounce (use for search)
function debounce(fn, delay){
let timeout;
return (...args)=>{
clearTimeout(timeout);
timeout = setTimeout(()=>fn(...args), delay);
};
}
✔ Throttle (use for scroll/resize)
function throttle(fn, delay){
let last = 0;
return (...args)=>{
const now = Date.now();
if(now - last >= delay){
last = now;
fn(...args);
}
};
}
