Search-as-you-type should call the API only after the user pauses typing. Technique?
Options
- Throttle
- Debounce (Correct option)
- Memoization
- Polling
Answer
Debounce
Why
Debounce waits until events STOP for a set time (e.g. 300 ms) and then runs once. Typing 'python' fast triggers one search, not six.
Throttle runs at most once per interval while events keep coming, good for scroll or resize handlers.
Also cancel outdated requests with AbortController so an old, slow response cannot overwrite a newer one.
Remember
Debounce = run after the pause. Throttle = run at most every N ms.