Skip to content
Shubham JangleResources
React

Explore topics

Auto-complete search box (Optimization)

Watch the walkthrough
Open on Instagram

Exercise: Optimize the autocomplete search input

Extend the previous autocomplete so rapid typing avoids redundant requests while the selected value and keyboard behavior remain correct. Use a configurable debounce delay, for example 300 ms, and a cache keyed by normalized query.

Requirements

  • Create one stable debounced request function and cancel its pending work on unmount.
  • Reuse cached results for repeated queries; define cache expiry or an explicit invalidation policy.
  • Cancel the previous fetch with AbortController or use a request identifier to ignore stale results.
  • Clear obsolete loading/error state when the query becomes too short, including during a pending debounce.

Acceptance checks and discussion

Type three letters rapidly: request only the final query after the pause. Delete and retype it: reuse a valid cached result. Explain why debounce alone does not solve response races.

The brief above is an original practice specification. The linked sandbox is supporting solution material and may implement a simpler subset of these requirements.

Supporting material