Sitemap
Muhammad Mozzammel Haque

Muhammad Mozzammel Haque

Senior Software Engineer in React & React Native, 6+ years. Portfolio: https://www.mozzammel.dev Coffee: buymeacoffee.com/mozzammel.dev

Member-only story

A debounced search box still ended on the wrong results in about 1 of 10 sessions. Aborting the old request fixed it

5 min read16 hours ago

I wanted to know what debounce actually does for a search-as-you-type box, so I stopped guessing and counted. Five ways of handling the keystrokes, 1,500 simulated typing sessions, one fake server. The short version: debounce cut requests by about a third and did nothing for the bug I cared about, which is an old response landing after a newer one.

Press enter or click to view image in full size
Timeline where a slower q11 reply arrives after q12 and overwrites it, despite debounce.

What I ran

Everything ran on Node 22.23.4 with lodash 4.18.1, on a 2-core Xeon at 2.1 GHz. The numbers are from that box only.

Each session types a 12-character query, one request per prefix length. The gap between keystrokes is random between 40 and 220 ms, and the debounce wait is 100 ms. The fake server answers each request after a random 60 to 400 ms. Keystroke times and server latencies are seeded, so every strategy saw exactly the same typing and the same delays. That’s 500 sessions with three seeds, so 1,500 sessions per strategy.

The five strategies:

  • no debounce and no abort, so every keystroke sends a request
  • lodash debounce(fn, 100)
  • a debounce I wrote myself, the usual clearTimeout and setTimeout pair

--

--

Senior Software Engineer at Arogga. 6+ years in React Native, React and Node.js. I run small benchmarks and write up the numbers. mozzammel.dev

See more from me