Performance & Best Practices — Web Animation

The budget of one frame (the frame budget) is 16.7 ms on a 60 Hz screen or 8.3 ms at 120 Hz. Inside it, the main thread does JavaScript, style, layout and…

An animation that is smooth on your laptop can stutter on a guest's cheap phone. Why? Remember the minibus from the requestAnimationFrame lesson. It leaves every 16.7 ms on a 60 Hz screen, or every 8.3 ms on a 120 Hz screen, and each departure is one frame. If your work for that frame isn't done when the minibus leaves, the minibus doesn't wait. That frame is missed, and your eyes see the animation stutter (jank). So a smooth animation means every frame is finished before the screen needs the next one. This lesson is about measuring where each frame's time goes, not guessing.

The budget of one frame

1,000 ms divided by 60 frames = 16.7 ms. Divided by 120 frames = 8.3 ms. That is the budget of one frame (the frame budget). On one laptop, we measured Chromium and Firefox calling rAF 120 times a second, and WebKit 60 times. Within that budget, the browser works through the sequence you already know: JavaScript, Style, Layout, Paint, Composite. Everything except composite happens on the main thread, which also runs your code and answers clicks.

A frame that takes 25 ms on a 120 Hz screen misses two departures, because the minibus leaves every 8.3 ms. On a cheap phone, the same work can be four to six times slower.

Measure first: the Performance panel

In Chrome DevTools (Chrome's tools for inspecting a page), open the Performance tab, also called the Performance panel:

  1. In the panel's settings, pick CPU: 4x slowdown. Your laptop's CPU (processor) runs 4 times slower, more like a phone.
  2. Click record, play the animation, then click stop.
  3. The Frames row marks frames that were missed or only partly drawn.
  4. The Main row shows the main thread's work: yellow for JavaScript, purple for Style and Layout, green for Paint. A task (a piece of the main thread's work) over 50 ms gets a red corner: that is a long task. Layout forced by JavaScript gets a "Forced reflow" warning.

The 4x figure is a rough guess. Since Chrome 134, pick Calibrate... in the CPU throttling menu (the setting that slows the CPU down). DevTools measures your laptop, then adds two presets (options) that are closer to low-tier and mid-tier phones.

CPU throttling also doesn't slow down the GPU process (the browser's part that uses the graphics chip), where composite happens. So test on a real phone from time to time with remote debugging. Connect an Android phone to the laptop by USB cable (USB debugging must be on in the phone), then open chrome://inspect on the laptop.

In the Rendering tab (Ctrl+Shift+P, type "rendering"):

Firefox has the Firefox Profiler, Safari the Timelines tab.

The Long Animation Frames API

The Long Animation Frames API (LoAF for short) tells your code about every frame over 50 ms, along with its cause. The code below sets up a PerformanceObserver (an observer of the browser's performance reports). It prints each long frame's length and its forced layout time:

new PerformanceObserver((list) => {
  for (const frame of list.getEntries()) {
    const forced = frame.scripts.reduce((ms, s) => ms + s.forcedStyleAndLayoutDuration, 0);
    console.log(`frame ${Math.round(frame.duration)} ms, forced layout ${Math.round(forced)} ms`);
  }
}).observe({ type: 'long-animation-frame', buffered: true });

For the loop that writes the photo heights of 300 cards (it's in the next section), Chromium 147 reported one 51 ms frame with 47 to 49 ms of forced layout. Support: Chrome 123 and later. Firefox and Safari: not yet (MDN data, October 2026). In other browsers, measure the time between frames yourself with requestAnimationFrame.

Layout thrashing and forced reflow

Writing a style (changing an element's CSS from JavaScript) makes layout dirty: the browser notes that layout must be computed again, but doesn't compute it yet. Reading a size after that (offsetWidth, offsetHeight, offsetTop, getBoundingClientRect(), getComputedStyle(), scrollTop) forces the browser to compute layout right then, so the answer is correct. That is a forced reflow (reflow is another name for computing layout). A loop that writes and reads in turn forces it on every pass: layout thrashing.

The two loops below set each photo's height to 0.75 times its width (a 4:3 ratio). photos holds the photo elements of the room cards.

// In turn: each pass writes, then the next pass reads
for (const p of photos) p.style.height = p.offsetWidth * 0.75 + 'px';

// Batched: read everything first, then write everything
const widths = photos.map((p) => p.offsetWidth);
photos.forEach((p, i) => (p.style.height = widths[i] * 0.75 + 'px'));

We ran both on 300 room cards:

In turn Batched
Chromium 147 300 layouts, 49 to 50 ms 1 layout, 0.3 to 0.4 ms
WebKit 26.4 69 to 73 ms under 1 ms
Firefox 146 128 to 134 ms 1 ms

The in-turn version computes layout 300 times, once per card. In Chromium, on a 120 Hz screen, those 50 ms mean 6 missed frames. The batched version does it only once. Even better without JavaScript: .photo { aspect-ratio: 4 / 3; } lets the browser set each photo's height from its width.

Cheap and expensive properties

Cheap means little work per frame. From the measurements in the Transform & Composite lesson (a card rising 16 px over 300 ms, Chromium 147): through top, layout ran 35 to 37 times. Through transform, once. A thickening box-shadow: 74 paints. transform and opacity are the properties that only need composite: layers that were already painted are just rearranged, and that work can run on the compositor thread. Animate those two, and avoid width, height, top, left, margin and padding.

will-change: one layer, one price

will-change: transform prepares a layer of its own for the element before the animation starts. The price is memory: each layer is a bitmap (a pixel picture) in GPU memory. A 328 × 143 px card on a 3x screen (each CSS px is drawn with 3 × 3 pixels) takes about 1.7 MB. 30 cards add 30 layers, about 51 MB. Putting it on every element as an "optimisation" makes too many layers: GPU memory runs out and composite actually slows down. Put it only on elements that really will move, briefly, then remove it.

Too many animations at once

Skeleton loading shows grey, empty cards while the data loads. Every card often gets a shimmer: a light band that sweeps across it again and again. We measured 300 skeleton cards over 2 seconds, with three ways to make the shimmer. In the table, ::after is an extra element that CSS creates:

Shimmer Chromium 147 Chromium, CPU 4x slower Firefox 146 WebKit 26.4 (60 Hz)
background-position on every card 241 frames; style 145 ms + paint 399 ms 83 frames, longest frame 617 ms 78 to 84 frames 122 frames
transform on every card's ::after 241 frames; style 239 ms, paint 0 compositor 240 frames, but rAF only 76 157 to 217 frames 122 frames
one ::after for the whole list 241 frames; style 18 ms, paint 0 241 frames 87 to 102 frames 121 frames

What the table shows: background-position is repainted on every frame. 300 animations mean 300 style updates per frame, even when they are only transform. And each browser is fastest with a different approach: Firefox draws more frames with transform on every card's ::after than with one ::after for the whole list. So measure first. Reduce the number of animations: shimmer only the visible cards, stop as soon as the data arrives, and switch it off when "Reduce motion" is on.

content-visibility: auto lets the browser skip the rendering work (style, layout and paint) for off-screen cards (Chrome 85, Firefox 125, Safari 26). With it, the style work of 300 background-position cards dropped from 145 to 52 ms. contain: layout paint (Chrome 52, Firefox 69, Safari 15.4) keeps changes inside a card from spreading over the whole page.

⚠️ Habits that make a frame miss the minibus

TL;DR: The budget of one frame (the frame budget) is 16.7 ms on a 60 Hz screen or 8.3 ms at 120 Hz. Inside it, the main thread does JavaScript, style, layout and paint. Measure, don't guess. Record in the Performance panel of Chrome DevTools with CPU 4x slowdown (a CPU 4 times slower), then look at the Frames and Main rows and the Forced reflow warning. From Chrome 123, the Long Animation Frames API reports long frames (over 50 ms) through duration, blockingDuration and scripts[].forcedStyleAndLayoutDuration. Reading offsetWidth (an element's width) after writing a style forces layout right then (a forced reflow). A loop that writes and reads in turn over 300 cards = 300 layouts (Chromium 49 to 50 ms, WebKit about 70, Firefox about 130). When everything is read first: 1 layout, under 1 ms. Animate transform and opacity (they can run on the compositor thread), not top (35 to 37 layouts) or box-shadow. will-change on every element = too many layers (one card on a 3x screen is about 1.7 MB). 300 animations at once weigh on the main thread even when they are only transform. Reduce the number, use content-visibility: auto (skip cards off screen), and measure in the browsers your visitors use.

⚡ The budget of one frame

Meaning: 1,000 ms divided by 60 frames = 16.7 ms, divided by 120 = 8.3 ms. That is the time to prepare one picture (a frame). Inside it, the main thread (the line of work that also runs your code) does JavaScript, style, layout (positions and sizes) and paint (colouring pixels). A frame that isn't done when the screen needs the next picture is missed, and the animation stutters.

⚡ Measure, don't guess

Meaning: Record in the Performance panel (Chrome DevTools) with CPU: 4x slowdown, so your laptop's CPU runs 4 times slower, like a phone. The Frames row marks missed frames, and the Main row shows the main thread's work, including the "Forced reflow" warning (layout forced by JavaScript).

⚡ Read everything first, then write

Write:

// In turn: each pass writes, then the next pass reads
for (const p of photos) p.style.height = p.offsetWidth * 0.75 + 'px';

// Batched: read everything first, then write everything
const widths = photos.map((p) => p.offsetWidth);
photos.forEach((p, i) => (p.style.height = widths[i] * 0.75 + 'px'));
Guess first: photos holds the photos of 300 room cards. Reading offsetWidth (the width) after writing a style forces the browser to compute layout right then. How many times does the in-turn version compute layout?

300 times, once per card, about 50 ms in Chromium. The batched version does it once, in under 1 ms.

Meaning: This forced layout calculation is called a forced reflow. The batched version reads everything first, then writes everything, so layout is computed only once.

⚡ Cheap properties

Meaning: transform and opacity only need composite (putting the picture layers together into one picture). Layers that were already painted are just rearranged, and that can run on the compositor thread (a separate line of work). Avoid animating width, height, top, left, margin and box-shadow.

What you will learn