$ ./demos --firehose
Firehose playground
Real-time UIs get more data than any screen can paint. This is a synthetic price feed running in your browser, with the switches I'd reach for and a meter showing what each one costs.
- Status
- idle
- Msgs handled/s
- 0
- Feed lag
- –
- Cost per render
- –
- FPS
- –
- p95 frame
- –
- Frames > 50 ms
- 0
- Long tasks
- 0
- Paints/s
- 0
- Msgs per paint
- –
- Last seq
- –
What to try
- Start the feed at 2k msg/s with Once per frame. The first four tiles say whether the page is keeping up: messages handled, how old the newest price on screen is, and what each render costs. The frame strip should sit on the dashed 16.7 ms line: several messages land per paint, and only the cells that changed are touched.
- Switch to Every message. Same feed, but the whole grid re-renders for each tick, which is what a top-level
setStatedoes to a tree with no memoisation. Watch the frame times and the feed lag climb. Raise the rate and the page falls behind for good; it pauses itself once it's 3 s behind. - Try 10 Hz. It's the cheapest option, but every price on screen can be up to 100 ms old. That's fine for a watchlist and wrong for the price you click to trade on.
- Compare DOM and canvas, and watch the first four tiles rather than FPS. With Once per frame at a few hundred symbols, the DOM keeps up easily: the browser already batches DOM writes into one style, layout and paint per frame, and a fast phone or laptop has plenty of headroom. Now make it heavy: 5,000 symbols at 50k msg/s. Each frame changes hundreds of cells, and the DOM pays style and layout for every one of them, while canvas draws only the rows on screen, so its cost per render stays flat however many symbols there are. Under Every message, both saturate the main thread and FPS looks equally bad, but canvas renders far more cheaply, so it handles several times more messages before it falls behind. Canvas costs you accessibility, text selection and find-in-page, so it suits charts, heatmaps and dense grids, not forms.
- Batch in the worker. One
postMessageper frame instead of one per tick. Message overhead is real at high rates, and batching is the browser version of Nagle's algorithm. - Drop packets. Every tick carries a sequence number. When one goes missing the page doesn't guess: it marks the grid stale, asks for a snapshot, and drops everything older than the snapshot when it arrives.
The ideas underneath
- Decouple the feed from rendering. Messages update a store; painting reads from it at the display's pace. Zustand transient updates do the same thing in React.
- Fix the update pattern before the renderer. Going from Every message to Once per frame beats any DOM-versus-canvas choice. Change the renderer when the view is dense enough that even one paint per frame is too much.
- Coalesce by key. Within a frame, only the latest price per symbol matters, so a burst of a hundred ticks on one symbol becomes a single write.
- Measure frames, not averages. A good mean hides the 200 ms stall a user actually feels; p95 and long frames show it.
- Recover by snapshot. Sequence numbers plus a snapshot are what make reconnects safe:message delivery semantics covers the delivery side, andgraceful degradation covers what to show while it heals.
Everything here is synthetic and runs locally: the feed is a random walk in a Web Worker, and no data leaves the page. More on the topic in Real-time UIs.