intermediate

JavaScript utility implementation

Implement debounce, throttle, memoize, EventEmitter, and Promise helpers with edge cases.

Utility implementation tasks test closure state, timers, callbacks, and API contracts. Debounce delays until calls stop; throttle limits call frequency; memoize caches by key; EventEmitter stores listeners; Promise helpers coordinate asynchronous completion and errors.

Trade-off: compare time, memory, and implementation complexity before committing to a structure or pattern.

On interviews: Interviewers watch edge cases: preserving this and arguments, cleanup methods, leading or trailing calls, listener removal during emit, and rejection handling.

Common pitfalls: A happy-path implementation is rarely enough. Define timing semantics before code, avoid leaking timers or listeners, and make cache keys explicit for memoize.

Checklist:

  • Define API contract.
  • Preserve this and args.
  • Clean timers and listeners.
  • Test errors and edge timing.