How does debouncing improve performance?

Debouncing prevents excessive function calls by delaying execution until the last call in a series of rapid events. It’s particularly useful for handling events like resizing or keystrokes.

function debounce(func, delay) {
  let timeoutId;
  return (...args) => {
    clearTimeout(timeoutId);
    timeoutId = setTimeout(() => func(...args), delay);
  };
}