Performant Animation — Performance

A smooth animation needs every frame done within 16.7 ms (60 fps). left, top, width and height run Layout and Paint on every frame: on a 1,500 element page…

On a mid-range phone, a box sliding back and forth stutters, even though the code is just one small animation. Change one CSS property and the motion turns smooth. The key is the time each frame takes.

An animation feels smooth when the browser can draw 60 frames every second. That means each frame has to be done within 16.7 ms (1,000 ms divided by 60). When a frame takes longer, it shows late, and the motion stutters. That stutter is called jank. Picture making a flipbook, a little book whose pages you flip fast. If you redraw the whole scene from scratch on every page, you can't keep up. If the character is drawn once on a clear plastic sheet, and on every page you just slide the sheet along, you can. Redrawing is left, sliding the sheet is transform.

60 frames per second

An ordinary screen shows a new picture 60 times a second. In between, the browser has 16.7 ms to get the next frame ready: running the Style, Layout, Paint and Composite you know from the CSS & Rendering Pipeline lesson.

left or transform, frame by frame

The Lab slides a box back and forth on a page with 1,500 elements. This is the cost of one frame (simulated numbers for a mid-range phone):

Step left transform: translateX()
Style 0.5 ms 0.5 ms
Layout 12.0 ms skipped
Paint 4.0 ms skipped
Composite 0.5 ms 0.5 ms
Total 17.0 ms, over 16.7 ms, so 30 fps 1.0 ms, 60 fps

From 17.0 ms down to about 1 ms. left changes the position in the layout, so on every frame the browser works out the whole page's Layout again and repaints the area that changed. transform doesn't touch layout: the box is drawn once into its own layer, and on every frame the GPU just slides that layer.

The cost of Layout grows with the number of elements. In the Lab it's 0.008 ms per element: on a 600 element page, left only costs 9.8 ms and still makes it. On a 3,000 element page, left costs 29.0 ms. transform stays at 1.0 ms however many elements there are.

One more advantage: a CSS animation of transform and opacity can be run by the compositor, off the main thread. So it stays smooth even while the main thread is busy running JavaScript.

Code: a smooth sliding box

<style>
  .box {
    width: 100px;
    height: 100px;
    background: #2563eb;
    will-change: transform;
    animation: slide 2s ease-in-out infinite alternate;
  }

  @keyframes slide {
    from {
      transform: translateX(0);
    }
    to {
      transform: translateX(200px);
    }
  }
</style>

<div class="box"></div>

The choppy version only differs in what's inside @keyframes:

.box {
  position: relative;
  animation: slide-bad 2s ease-in-out infinite alternate;
}

@keyframes slide-bad {
  from { left: 0; }
  to { left: 200px; } /* Layout and Paint on every frame */
}

Rules of thumb

To make it Use Avoid
Move transform: translate() left, top, margin
Grow or shrink transform: scale() width, height
Rotate transform: rotate()
Fade in or out opacity visibility, display

will-change: only where needed

Every layer takes GPU memory, roughly width × height × 4 bytes (and more again on phone screens with a high pixel density). A 100 × 100 px box is only 40 KB. But 200 cards of 400 × 300 px, all given will-change, need about 96 MB. A cheap phone can run out of memory, and the page gets slower instead.

⚠️ Where does jank usually come from?

TL;DR: A smooth animation needs every frame done within 16.7 ms (60 fps). left, top, width and height run Layout and Paint on every frame: on a 1,500 element page, left costs 17.0 ms and drops to 30 fps. transform and opacity only need Composite: 1.0 ms, however many elements there are. Write the motion in @keyframes with transform: translateX(), run it with animation: slide 2s ... (name first), and give will-change: transform only to elements that are really animated.

⚡ 16.7 ms per frame

Meaning: An animation feels smooth when the browser can draw 60 frames a second, so every frame has to be done within 16.7 ms. A late frame makes the motion stutter, which is called jank.

⚡ left or transform

Guess first: which properties are the safest to animate, because they only trigger Composite?

transform and opacity. In the Lab, one frame with left costs 17.0 ms and drops to 30 fps, while transform takes just 1.0 ms.

Meaning: left, top, width and height run Layout and Paint on every frame. transform draws the box once into its own layer, then the GPU just slides that layer along.

⚡ A smooth sliding box

Write:

.box {
  will-change: transform;
  animation: slide 2s ease-in-out infinite alternate;
}

@keyframes slide {
  from { transform: translateX(0); }
  to { transform: translateX(200px); }
}

Meaning: The animation's name comes first. Give will-change only to elements that are really animated, because every layer takes GPU memory.

What you will learn