08

Find the Invisible Cost: a CSS Animation and a Shared Paint Layer

Two of the biggest wins in the lab came from code nobody suspected: two bouncing navigation arrows, and the order of layers inside one SVG. Day 8 shows the instruments that found them and why each fix changes nothing a child can see.

A forced recalculation in every frame

By this point, the remaining JS cost looked spread out. So web-trace.mjs gained a new lens: forced style and layout. It lists every style recalculation or layout that ran inside script (in a rAF callback or an event handler), grouped by the script that triggered it, with --stacks for the line of code.

forced style/layout (inside script), low-end playground drag:
   334 ms   403x  UpdateLayoutTree in FireAnimationFrame @ render

Almost every frame, the first layout read in render() (the one Day 3 deliberately moved to the start of the frame) was forcing a style recalculation. Something was invalidating style between frames.

Bisecting with CSS

Rather than guessing, the trace was rerun with CSS overrides injected into the page:

as is                                         294 ms forced
*{animation:none; transition:none}              2 ms
[data-zone-arrow]{animation:none}              17 ms
[data-zone-arrow]{will-change:transform}      276 ms

The culprits were the two ‹ › zone arrows, which bob gently to invite a child to the next area of the world: an infinite CSS keyframe animation. On this machine it ticked on the main thread and invalidated style before every frame, so the stage's first read paid for a whole style pass, and the browser then did another after the frame's writes.

The fix keeps the exact same motion: a 6 px bob every 1.8 s, now written from the stage's own frame loop, after the view's reads. Disabled arrows are not touched, and reduced-motion preferences still keep them still. A browser check sampled the arrow's position every frame for 3.6 s in both builds: amplitude 6 px vs 6 px, period 1 800 ms vs 1 800 ms, curves within 0.11 px.

One debugging detour is worth telling. The first browser check reported the new arrow not moving at all. The cause was the check, not the code: the test page left the stage below the fold, and the stage's clock pauses when the stage is off screen, so the bob paused too. With the stage in view the curves matched. Lesson: when a fix appears to break something, check the harness before the fix.

A paint layer shared with things that move

The paint phase was still the largest main-thread phase. A layer ablation (hiding one layer at a time with CSS and tracing each) split it:

hidden layerpaint, low-end playground drag
nothing2.18 s
the moving cast1.04 s
the static backdrop1.58 s
the static scenery and tiles1.69 s

The backdrop and scenery never move, yet each cost about half a second of paint. They shared one SVG with the moving cast, and every frame the browser re-recorded the whole SVG's display list.

A prototype moved the static layers into their own SVG directly underneath, using a page snippet (web-trace --eval) before writing any product code. Paint dropped by about half. The real change creates an underlay SVG: backdrop, night sky, rainbow, scenery and its tiles. It has the same viewBox, aria-hidden, no pointer events, and sits under the stage SVG. An SVG whose content did not change is not repainted.

The first version was off by a fraction of a pixel: it sized the underlay with clientWidth, which rounds, and re-sampled the whole world's scenery. Sizing it from the exact fractional box made the static world pixel-identical in three worlds at three scroll positions. Photos taken with the in-game camera re-include the underlay, and taps were unaffected, because a tap on the backdrop never resolved to an object anyway.

Before and after side by side

The zone arrows (EXP-031), low-end playground:

Zone arrows: HUD before vs after

The underlay (EXP-033): before 43–58 fps with 22–24 % dropped; after 49–51 fps with 15–17 % dropped.

Underlay: HUD before vs after

low-end, interleaved, 2 runsbeforeafter
forced style recalc per drag trace334 / 333 ms10 / 15 ms
playground swipe, dropped76 / 81 %36 / 37 %
playground drag-slow, dropped49 / 43 %20 / 24 %
paint, playground drag (underlay)2.09 / 2.06 s1.09 / 1.06 s
paint, playground swipe (underlay)1.57 / 1.53 s0.72 / 0.71 s
bench dropped-frame sum (underlay, 24 cells × 2)639462 (−28 %)

Design principles at work

  • Separate what changes from what doesn't. The underlay is the rendering version of the single-responsibility principle: one layer for the static world, one for what moves, so a change in one never forces work in the other.
  • Single source of time. The arrows now take their time from the stage's frame loop like every other animation, so they pause with the stage, respect the same order of reads and writes, and cannot invalidate style behind its back.
  • Prototype through the instrument. --eval and --css let an idea be measured in the real page before any product code is written.

Key takeaways

  • Trace forced style and layout with stacks; the first read of a frame often pays for someone else's invalidation.
  • Bisect with CSS overrides before touching code.
  • Static and moving content should not share a paint layer; split them and prove pixel parity.
  • When a fix seems to break something, suspect the measurement harness first.