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
Decision Framework
| Use Throttle When | Use Debounce When |
|---|---|
| You need regular updates during activity | You only care about final state |
| User expects visual feedback during action | User expects action after they stop |
| Examples: scroll position, resize, drag | Examples: search input, form validation |
Follow-up Questions
- How would you explain this to a junior engineer in a code review?
- Are there cases where you might use both together?
- How do you handle edge cases in animations/games?