Experiments · JavaScript: From Closures to Autonomous Systems · Chapter 13

Search as you type

The typeahead race condition and RxJS switchMap

Theme
Try itSearch as you type
Every letter typed into the search box sends a request, and the server answers in 20–300 ms in no particular order. Type “meteor” yourself or with the button and see whose answer stays on screen: with promises it is often a stale one, with switchMap always the latest. A hundred runs in a row give a score to compare with the book.

Everything is computed in your browser; nothing is sent anywhere.

About this experiment

What does switchMap do in RxJS?

switchMap is an RxJS operator that turns each value of a source stream into a new inner stream and keeps only the latest one. When a new value arrives, it unsubscribes from the previous inner stream before subscribing to the next. In RxJS unsubscribing is cancellation: a timer is cleared, a request made with RxJS ajax is aborted. That makes switchMap the usual answer for search as you type, where every keystroke starts a request and only the answer to the latest query should reach the screen.

A search box that asks the server on every keystroke sends its requests in order, and the answers come back in whatever order the network likes. If the code shows each answer as it arrives, the screen ends up with whichever came last, and that can be the answer to a query the user typed two letters ago.

Press “Type ‘meteor’”: six letters go in 50 ms apart, as in the book’s experiment, and each one sends a request to a fake server that answers after 20 to 300 ms. The latencies come from the book’s seeded generator, so a given seed always gives the same delays; change the seed to get another run, or type yourself. The timeline shows each request’s bar and when its answer lands. On the switchMap tab every new letter aborts the previous request with an AbortController, which is what switchMap does by unsubscribing, and the cancelled bars stop short. The “100 runs” button replays seeds 1 to 100 in both modes on real timers.

In the book, with RxJS 7.8.2 on Node 24, the promise version showed “meteor” in 55 runs out of 100. In the other 45 the screen kept a stale answer such as “mete” or “meteo”, and all six requests ran to the end. With switchMap the screen showed “meteor” every time, and three or four of the six requests were cancelled before they answered.

The chapter recalls that a promise cannot be cancelled, so cancellation has to be passed along by hand. A stream in RxJS cancels itself once nobody needs it. RxJS left the programmer the layout of the pipeline and took time, order and cancellation on itself.

From the book

JavaScript: From Closures to Autonomous Systems · Chapter 13

From MVC to the Reactive Graph

Next to this experiment in the chapter
The more the runtime decides, the cheaper it is to write code, and the harder it is to understand why something was recomputed or, conversely, was not. The dependency graph Tracker builds on the fly is written down nowhere, and understanding it requires separate tools. In the next chapter Meteor will stretch this graph across the network. Data lying in a database on the server will become reactive sources for functions running in the user’s browser, and the question «who depends on whom» will become a question of who has the right to see which data.