Skip to content
matthewcm.dev

    ↑ ↓ move · ↵ open · tab scopeopen graph →

    $ ./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.

    Render strategy
    Renderer
    Delivery
    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

      1. 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.
      2. Switch to Every message. Same feed, but the whole grid re-renders for each tick, which is what a top-level setState does 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.
      3. 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.
      4. 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.
      5. Batch in the worker. One postMessage per frame instead of one per tick. Message overhead is real at high rates, and batching is the browser version of Nagle's algorithm.
      6. 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

      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.