Skip to content

Commit 091c028

Browse files
authored
Create Throttle vs Debounce.md
1 parent 28ad60f commit 091c028

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

FE75/Throttle vs Debounce.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
| Aspect | Throttle | Debounce |
2+
| :-- | :-- | :-- |
3+
| Callback Execution | At most once every specified time interval (wait). | Only once after the events stop firing for a specified delay. |
4+
| Execution Timing | The callback is invoked periodically during events (e.g., every X ms). | The callback is invoked after the events have "settled down." |
5+
| Use Case | Continuous/periodic actions (e.g., scroll tracking, progress bar update). | One-time actions after event bursts (e.g., API search request). |
6+
| When the Callback Fires | Fires immediately on first event and then throttles subsequent executions. | Fires only after the timer runs out following the last event. |
7+
| Effect on Performance | Limits the number of function calls to reduce resource usage. | Prevents execution during bursts, reducing resource consumption further. |
8+
9+
10+
### Throttle
11+
#### Definition: Throttle ensures that a function executes at most once in a specified time interval, no matter how many times an event occurs.
12+
13+
#### Behavior:
14+
15+
* The callback function is executed immediately on the first trigger.
16+
* Once invoked, the function will not execute again until a specified wait time has elapsed, even if the event is triggered continuously during the waiting period.
17+
* It evenly spaces out the execution of the function over time.
18+
19+
#### Use Case: Use throttle when you want to handle events periodically at a controlled rate. It's helpful for tasks that need regular updates but don't need to react to every event. Examples:
20+
21+
* Scrolling: Adjust a progress bar or navigation indicator.
22+
* Resizing: Adjust layout or perform calculations while resizing the window.
23+
* Repeated API calls: Prevent sending too many API calls in a short time.
24+
25+
### Debounce
26+
#### Definition: Debounce ensures that a function executes only after a specified delay has passed since the last event in a sequence.
27+
28+
#### Behavior:
29+
* The callback function will only execute if the event stops being triggered for the specified delay.
30+
* If a new event occurs within the wait period, the timer restarts (resetting the delay).
31+
* It delays execution until the burst of events is complete.
32+
33+
#### Use Case: Use debounce when you want to wait until the event "settles down". It's useful for tasks that should not be executed repeatedly but only after the user's input or action is complete. Examples:
34+
35+
* Search Input: Fire a search query to an API only after the user stops typing (e.g., in search boxes).
36+
* Resize: Perform expensive layout recalculations after the user has finished resizing the window.
37+
* Form Validation: Validate fields only once the user has stopped entering data.

0 commit comments

Comments
 (0)