Skip to main content
InterviewHubPRO
Section Overview
Q532

Question 532 of 536

Part 8: Modern JavaScript APIs, WeakRef, Proxy & Performance (464 - 536)

99%
JavaScript & V8 Internals (536 Qs)•Part 8: Modern JavaScript APIs, WeakRef, Proxy & Performance (464 - 536)

How do you implement advanced debounce and throttle functions with leading/trailing edge execution and cancellation?

mid LevelMost AskedCode ExampleFormsPerformanceHTTP & REST APIs

In Simple Words: Debounce postpones execution until a specified delay has elapsed since the last invocation, while throttle guarantees that a function executes at most once per defined time window, both optionally supporting immediate (leading) and delayed (trailing) edge triggers.

1. Advanced Debounce with Immediate (Leading) & Cancellation

javascript
function debounce(fn, wait, options = {}) {
  let timerId = null;
  let lastArgs = null;
  let lastThis = null;

  const leading = Boolean(options.leading);
  const trailing = options.trailing !== undefined ? Boolean(options.trailing) : true;

  function debounced(...args) {
    lastArgs = args;
    lastThis = this;

    const shouldCallNow = leading && !timerId;

    if (timerId) {
      clearTimeout(timerId);
    }

    timerId = setTimeout(() => {
      if (trailing && !shouldCallNow) {
        fn.apply(lastThis, lastArgs);
      }
      timerId = null;
      lastArgs = null;
      lastThis = null;
    }, wait);

    if (shouldCallNow) {
      fn.apply(lastThis, lastArgs);
    }
  }

  // Cancel pending timer
  debounced.cancel = function () {
    if (timerId) {
      clearTimeout(timerId);
      timerId = null;
    }
    lastArgs = null;
    lastThis = null;
  };

  // Immediately invoke pending execution
  debounced.flush = function () {
    if (timerId && lastArgs) {
      fn.apply(lastThis, lastArgs);
      debounced.cancel();
    }
  };

  return debounced;
}

2. Advanced Throttle with Leading & Trailing Control

javascript
function throttle(fn, wait, options = {}) {
  let timerId = null;
  let lastArgs = null;
  let lastThis = null;
  let lastCallTime = 0;

  const leading = options.leading !== undefined ? Boolean(options.leading) : true;
  const trailing = options.trailing !== undefined ? Boolean(options.trailing) : true;

  function throttled(...args) {
    const now = Date.now();
    lastArgs = args;
    lastThis = this;

    if (!lastCallTime && !leading) {
      lastCallTime = now;
    }

    const remaining = wait - (now - lastCallTime);

    if (remaining <= 0 || remaining > wait) {
      if (timerId) {
        clearTimeout(timerId);
        timerId = null;
      }
      lastCallTime = now;
      fn.apply(lastThis, lastArgs);
      lastArgs = null;
      lastThis = null;
    } else if (!timerId && trailing) {
      timerId = setTimeout(() => {
        lastCallTime = leading ? Date.now() : 0;
        timerId = null;
        fn.apply(lastThis, lastArgs);
        lastArgs = null;
        lastThis = null;
      }, remaining);
    }
  }

  throttled.cancel = function () {
    if (timerId) {
      clearTimeout(timerId);
      timerId = null;
    }
    lastCallTime = 0;
    lastArgs = null;
    lastThis = null;
  };

  return throttled;
}