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
EasyJavaScript

Implement a Basic Throttle Function

141 views

Problem Statement

As a Staff Engineer reviewing production performance, I frequently encounter scenarios where we need to limit the rate of function execution—especially for high-frequency events. Implement a throttle utility that ensures a function executes at most once per specified time interval.

Requirements

Implement a throttle(func, wait) function that:

  • Executes func immediately on the first call
  • Ignores subsequent calls within the wait period
  • After the wait period, the next call executes immediately
  • Preserves this context and arguments

Example Usage

const trackScroll = (scrollPos) => {
  console.log(`Scroll position: ${scrollPos}`);
};

const throttledScroll = throttle(trackScroll, 100);

// User scrolls rapidly (event fires every 10ms)
// t=0ms:   throttledScroll(0);   // Logs: "Scroll position: 0"
// t=10ms:  throttledScroll(50);  // Ignored
// t=20ms:  throttledScroll(100); // Ignored
// ...
// t=100ms: throttledScroll(500); // Logs: "Scroll position: 500"
// t=110ms: throttledScroll(550); // Ignored
// t=200ms: throttledScroll(900); // Logs: "Scroll position: 900"

Key Difference from Debounce

ThrottleDebounce
Executes at regular intervalsExecutes after activity stops
Guarantees execution during activityOnly executes when activity pauses
Use for: scroll, resize, mousemoveUse for: search input, form validation

Real-world Context

  • Scroll event handlers for infinite scroll or parallax
  • Mouse move handlers for drag operations
  • Rate-limiting API calls
  • Game loop input handling

Follow-up Questions

  1. What happens to the last call if it falls within a throttle window?
  2. How would you ensure the last call is always executed?

Sample Test Cases

Case 1
Input
{"delay": 1000, "calls": [0, 100, 200, 1500, 1600]}
Expected Output
[0,1500]
Case 2
Input
{"delay": 500, "calls": [0, 100, 600, 700, 1200]}
Expected Output
[0,600,1200]
Case 3
Input
["func", 100, [{"time": 0, "args": [1]}, {"time": 50, "args": [2]}, {"time": 100, "args": [3]}, {"time": 150, "args": [4]}, {"time": 200, "args": [5]}]]
Expected Output
[{"time": 0, "args": [1]}, {"time": 100, "args": [3]}, {"time": 200, "args": [5]}]
Case 4
Input
["func", 50, [{"time": 0, "args": ["a"]}, {"time": 10, "args": ["b"]}, {"time": 20, "args": ["c"]}, {"time": 50, "args": ["d"]}, {"time": 60, "args": ["e"]}, {"time": 100, "args": ["f"]}]]
Expected Output
[{"time": 0, "args": ["a"]}, {"time": 50, "args": ["d"]}, {"time": 100, "args": ["f"]}]
Case 5
Input
["func", 200, [{"time": 0, "args": [10]}, {"time": 199, "args": [20]}, {"time": 200, "args": [30]}, {"time": 201, "args": [40]}, {"time": 399, "args": [50]}, {"time": 400, "args": [60]}]]
Expected Output
[{"time": 0, "args": [10]}, {"time": 200, "args": [30]}, {"time": 400, "args": [60]}]

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
141
Likes
0
Solutions
0
Comments
0

Category

Frontend Engineering