DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.

← Back to Questions
HardJavaScript

Design a Rate Limiter: Throttle vs Debounce Decision Framework

41 views

Problem Statement

As a Staff Engineer, you're reviewing a PR where a junior engineer used debounce for a scroll handler. Walk through your decision framework for choosing between throttle and debounce, and implement both to demonstrate the difference.

Requirements

Implement both functions and create a demonstration that clearly shows:

  • How throttle guarantees regular execution during continuous input
  • How debounce waits for input to stop
  • Visual output showing execution timing for both

Example Implementation

// Your implementations
function throttle(func, wait) { /* ... */ }
function debounce(func, wait) { /* ... */ }

// Demonstration
function runComparison() {
  const events = [];
  const start = Date.now();
  
  const logThrottle = throttle((v) => {
    events.push({ type: "throttle", time: Date.now() - start, value: v });
  }, 100);
  
  const logDebounce = debounce((v) => {
    events.push({ type: "debounce", time: Date.now() - start, value: v });
  }, 100);
  
  // Simulate 500ms of continuous input (every 30ms)
  let value = 0;
  const interval = setInterval(() => {
    value++;
    logThrottle(value);
    logDebounce(value);
  }, 30);
  
  setTimeout(() => {
    clearInterval(interval);
    // Wait for debounce to complete
    setTimeout(() => {
      console.table(events);
      /*
      Expected output:
      | type     | time | value |
      |----------|------|-------|
      | throttle | 0    | 1     |
      | throttle | 100  | 4     |
      | throttle | 200  | 7     |
      | throttle | 300  | 10    |
      | throttle | 400  | 14    |
      | throttle | 500  | 17    |
      | debounce | 600  | 17    |  ← Only fires once, after input stops!
      */
    }, 150);
  }, 500);
}

Decision Framework

Use Throttle WhenUse Debounce When
You need regular updates during activityYou only care about final state
User expects visual feedback during actionUser expects action after they stop
Examples: scroll position, resize, dragExamples: search input, form validation

Follow-up Questions

  1. How would you explain this to a junior engineer in a code review?
  2. Are there cases where you might use both together?
  3. How do you handle edge cases in animations/games?

Sample Test Cases

Case 1
Input
{"scenario": "scroll", "pattern": "continuous", "needImmediate": true}
Expected Output
throttle
Case 2
Input
["throttle", 100, [0, 50, 120, 180, 250, 300, 380, 450, 520], 500]
Expected Output
{"calls": [{"time": 0, "value": 0}, {"time": 100, "value": 2}, {"time": 200, "value": 4}, {"time": 300, "value": 6}, {"time": 400, "value": 8}, {"time": 500, "value": 9}]}
Case 3
Input
{"scenario": "search", "pattern": "burst", "needImmediate": false}
Expected Output
debounce
Case 4
Input
["debounce", 100, [0, 50, 120, 180, 250, 300, 380, 450, 520], 650]
Expected Output
{"calls": [{"time": 620, "value": 8}]}
Case 5
Input
["throttle", 50, [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100], 150]
Expected Output
{"calls": [{"time": 0, "value": 0}, {"time": 50, "value": 5}, {"time": 100, "value": 10}]}
Case 6
Input
["throttle", 100, [0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500], 50]
Expected Output
{"calls": [0, 100, 200, 300, 400, 500]}
Case 7
Input
["debounce", 100, [0, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500], 50]
Expected Output
{"calls": [600]}
Case 8
Input
["throttle", 200, [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250, 260, 270, 280, 290, 300, 310, 320, 330, 340, 350, 360, 370, 380, 390, 400, 410, 420, 430, 440, 450, 460, 470, 480, 490, 500], 10]
Expected Output
{"calls": [0, 200, 400]}

No solutions yet

Be the first to share a solution for this question.

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Stats

Views
41
Likes
0
Solutions
0
Comments
0

Category

Frontend Engineering