The TiffinBox BuildOne system, every chapter
0 / 0

Workbench / The TiffinBox Build

S18 · V2·18

Profiling the Display

Volume 2, chapter 18. The stage 17 kitchen display, measured with 500 open orders on a CPU slowed four times: applying the first snapshot took 1,482 ms in one task, and Long Animation Frames named the culprit (`EventSource.onsnapshot`). The cause was in stage 17's own code: every rendered order refreshed every card's age, a quadratic loop, plus a selector search per update. Three measured changes (one refresh per frame, cards in a `Map`, `content-visibility`) brought it to 29 ms, with named performance measures left in the code for the tests and for real-user monitoring later.

6 parts · ~5 min to read
Updated Suggest a correction

Stage 18: profiling the display

The brief

What you'll build.

  • named timings in kitchen.ts: performance.mark/measure around applying a snapshot (tb:snapshot), one order event (tb:order) and the age refresh (tb:tick);
  • web-tests/stage18.py: a server with N open orders (500 by default), the display loaded in Chromium with CPU throttling at 4× (Emulation.setCPUThrottlingRate), long tasks and Long Animation Frames observed from page start, then 10 new orders and one age refresh; it prints time to live, the named timings, and the worst frame with its script attribution;
  • three fixes, each measured on its own: one age refresh per frame, cards found through a Map, and content-visibility: auto on cards (with writes skipped when nothing changed).

Why a real company needs this. Code that's fine with 10 orders in a test is a different program with 500 orders on a ₹12,000 tablet. Performance problems hide in loops that look harmless, and only measurement under realistic conditions finds them. Leaving named measures in the code makes the next regression visible without a profiler.

Why now. Chapter 18 is browser internals: the rendering pipeline, frames, forced layout, the engine and containment. The display from stage 17 is the first page with enough elements and updates to need it.

What you'll have at the end.

  • a repeatable profile of the display at any size;
  • snapshot apply from 1,482 ms to 29 ms at 500 orders, new orders from 6.0 ms to 1.1 ms;
  • ADR-0020: performance measurement in the code.

Theory to own

  1. Throttling. Why measure with the CPU slowed 4×, and what doesn't throttling simulate?
  2. Long tasks and frames. What's a long task, what does Long Animation Frames add, and what does blockingDuration mean?
  3. Complexity in UI code. A function that touches every card, called once per card. Why is that easy to write and hard to notice?
  4. content-visibility: auto. What does it skip, what does contain-intrinsic-size do, and what stays available to assistive technology and find-in-page? (Chapter Exercises P8.)
  5. Named measures. Why put performance.mark calls in shipped code rather than profiling when needed?

Build it

Step 1: name the work

Add performance.mark('tb:snapshot:start') and performance.measure('tb:snapshot', 'tb:snapshot:start') around applying a snapshot; the same for one order event and for tick (the age refresh).

Checkpoint. In DevTools' Performance panel, a recording shows tb:snapshot in the Timings track.

Step 2: a realistic profile

Write web-tests/stage18.py: place N orders for kitchen 7; open /kitchen/k7 with CPU throttling at 4× and observers for longtask and long-animation-frame registered before the page's own scripts (add_init_script); wait for "Live"; print the page size, time to live, tb:snapshot, and the worst frame with its scripts; then place 10 orders and print tb:order; wait for the next 15-second refresh and print tb:tick.

Checkpoint. A baseline at 500 orders.

Step 3: one refresh per frame

Replace the tick() call at the end of render() with scheduleTick(), which queues at most one tick per animation frame with requestAnimationFrame.

Checkpoint. Re-run the profile.

Step 4: find cards without searching

card(id) used list.querySelector('li.ticket[data-order-id="…"]'): a search through every card, once per order. Keep a Map<string, HTMLLIElement> filled from the server-rendered cards and updated on insert and removal. In tick, iterate the Map, and only write text and classes that changed.

Checkpoint. Re-run the profile.

Step 5: skip what nobody can see

Add content-visibility: auto; contain-intrinsic-size: auto 11rem; to .ticket. Re-run the profile, then the stage 14 audit and the stage 17 browser test.

Checkpoint. Every number is at least as good, and the audits still pass.

Senior and architect lens

1. Measure at the size that matters, on the device that matters. The stage 17 code passed every test at 3 orders. At 500 on a throttled CPU, it froze for 1.5 s. Performance tests need production-sized data and realistic hardware profiles as a fixed part of the suite, not a one-off investigation.

2. ADR-0020: performance measurement in the code. Write it with these points:

  • Decision: user-visible operations carry named measures (tb:<area>:<operation>); tests and real-user monitoring read them; no measure, no performance claim.
  • Profiles: stage18.py-style profiles at realistic sizes (500 open orders for the display) with 4× CPU throttling run in CI; budgets on the named measures (tb:snapshot under 100 ms, tb:order under 10 ms) fail the build when exceeded (stage 27 formalises budgets).
  • Patterns: coalesce per-item work into one per frame; keep keyed indexes instead of searching the DOM; write only changes; content-visibility for long lists.
  • Revisit when: a page needs more than one screen of interactive items updating often (virtualisation, stage 21).

3. Attribution beats intuition. Before the profile, the natural suspects were the network, JSON parsing or the rendering engine. Long Animation Frames pointed at EventSource.onsnapshot in one line, and the named measures split it into snapshot, order and tick. Guessing would have optimised the wrong thing.

4. Quadratic behaviour hides in composition. No single function here was slow. render was fine, tick was fine; render calling tick was N². Code review for performance means asking "how many times does this run, as a function of N?" at every call that touches a collection.

5. Keep the measurement after the fix. The marks stay in the shipped code. When a later change reintroduces a slow path, the same profile, or a dashboard of real devices, shows it at once.

What breaks first, and when it's addressed:

Pressure What breaks Where it's addressed
the HTML for 500 cards is 136 KB and parsing is now the longest task server renders every card stage 21 (render the first screen, stream the rest)
thousands of cards (a central kitchen) every card exists in the DOM stage 21 (virtualised list)
no budget fails the build when a measure regresses numbers are printed, not enforced stage 27 (performance budgets in CI)
real tablets differ from 4× throttling lab numbers only stage 32 (named measures collected from real devices)
the server's own costs (snapshot JSON for 500 orders) only the browser was profiled Volume 3 (server profiling with JFR)

Evidence

  • stage18.py output for the baseline and each step is in the README (or the median of three runs each).
  • The named measures (tb:snapshot, tb:order, tb:tick) are in the code and visible in a DevTools recording.
  • check-js.sh, stage14.py (failures: 0) and stage17.py still pass.
  • ADR-0020 is committed.

Carry forward

  • The named measures are what stage 27's budgets and stage 32's real-user monitoring report.
  • The profile at realistic N joins the CI suite; stage 27 runs it on every change to the display, with budgets.
  • The patterns (per-frame coalescing, keyed indexes, write-only-changes) are the defaults for every list in stages 21–25. content-visibility isn't: stage 20 found it hides off-screen cards' content from screen readers in Chromium, and replaces it.