Engineering · Speed

The menu asked for itself before the page had arrived

A guest page that downloads a quarter of a megabyte before it asks for the menu pays the round trip in series. Twenty lines in the head start it first.


The page a guest lands on from a QR code is a single file with the whole ordering app inside it, plus twenty-nine small scripts that carry the rules it needs: how a meal deal prices, how a hall splits one basket, what a kiosk is allowed to do. Over the wire that is about ninety kilobytes of page and a hundred and fifty of scripts, compressed, over thirty requests. A phone on a decent connection has it in under a second; a phone in a busy hall at lunchtime can take several.

Only once all of that had downloaded and run did the page ask the server for the menu. The code that asked sat at the very bottom of the file, after every script it depended on, which is where it has to sit to be able to draw the answer. So the one network call the guest actually came for was paid in series, after the shell, and on a slow phone the menu’s own round trip was the last thing to happen rather than the first.

The change is twenty lines in the head of the page, before the first script. It works out the same request the boot at the bottom would make — the venue from the page’s own address, the table from the link or from the last visit, which menu a multi-menu venue printed on this code — and starts the fetch, keyed by the exact path. When the boot reaches the same request it finds the answer already in flight, takes it once, and carries on. If the key differs by a character, the boot simply fetches for itself and the early one is a wasted request rather than a wrong menu. If the early fetch failed, the boot asks again. The demo pages, which answer from a fixture, start nothing.

Measured on a local copy of the venue with the whole shell arriving in about a hundred and fifty milliseconds: the menu request used to go out between 137 and 175 milliseconds after the page was requested, which was the moment the last script finished. It now goes out between 30 and 56. On a laptop that is a rounding error. On a phone where the shell takes three seconds, the gap the menu used to wait behind is the whole three seconds, and now it waits behind none of it.

The test for this does not read the two pieces of code and check they look alike. It runs the head script in a sandboxed window with a plain scan, a scan with a table and a menu slug, a venue host with a remembered table, a demo address, and a storage object that throws. Then it evaluates the boot’s own path expression with the same inputs and checks the two strings are identical. Either side can change its mind about the order of the parts, and the test fails by name rather than letting the two drift apart and the early answer go quietly unused.


Try it on tonight’s service.

Nothing to install, no card. Not better by the weekend? Close the tab.