Motion Performance Lab
Magazine/Feature

The surprising findings of Motion Performance Lab

Can you teach an old dog new tricks? I am an old dog, so to find out I've built the Motion Performance Lab.

Hardware acceleration is treated as the holy grail of web animation performance. And with good reason! Stick to transform, opacity, clipPath and filter, and your animations will (probably) run smoothly, even if your CPU is busy with other work.

Motion's hybrid engine combines the browser's accelerated animation APIs with its own fast JavaScript driver. Using the former's GPU performance where possible but never limiting you to its capabilities. We spend good kilobytes on this being core to our library (not a setting or separate import) because the performance benefits are so clear.

Historically, we've steered away from changes where the performance benefits aren't clear. It's difficult to spin up reliable testing across multiple devices, and often results are noisy.

That's why we're building the Motion Performance Lab. A set of high and low power devices that we're collecting and calibrating to better detect worthy optimisations.

Automatically, the Lab runs every new Motion branch through a series of benchmarks, allowing us to monitor changes, test new ideas, and catch performance regressions.

To find results that are statistically significant we have our Head of ML and Statistics here at Motion, aka Dr Mrs Perry (my wife) - a machine learning researcher with a PhD and a masters in Maths from the actual Cambridge. She is crucial in setting correct baselines, understanding variance and more.

Some of our initial learnings from the Performance Lab are out in Motion 15, including adaptive acceleration, which keeps animations smooth across more devices and workloads.

But the fun part about the Performance Lab has been making discoveries that go against conventional wisdom. Some of it has nailed down, with numbers, things I had a vague idea of, and some of it has been downright surprising.

So what are they? Let's take a look.

Surprise! Hardware acceleration can make your animations slower

Conventional wisdom is that hardware accelerated animations are always better. But actually, there are situations in which they can make your animations slower.

A simple test animating transform (which can animate entirely on the GPU/compositor) and border-radius (which must trigger a paint via the CPU/main thread):

  • CSS: The transform naturally runs on the compositor with border-radius animating on the main thread.
  • JavaScript: Animating the same values via element.style in a requestAnimationFrame loop, everything on the main thread.

By firing this animation up in Performance Lab, we can see that on both high and low-end devices that when we increase the number of elements, CSS performance drops off while the JavaScript animation continues at the device's max frame rate of 120fps.

FRAMES PER SECONDELEMENTS, 1,000 TO 4,000120FPS (MAX)JS120FPSCSS70FPS
Translate with paint (Chrome)
FRAMES PER SECONDELEMENTS, 50 TO 50060FPS (MAX)JS60FPSCSS21FPS
Translate with paint (Chrome low-end)

What's happening here?

Hardware accelerated animations are considered to perform better than the main thread, not because they have higher frame rates (they don't) but because of their consistently higher frame rates.

The way they work is, browsers cut elements out of the page into separate snapshots called layers which are uploaded to the GPU for rendering and animation.

An accelerated animation can play without being affected by workloads on the CPU. Look at the performance characteristic of the transform and border-radius animation if we throw 25ms of main thread work per frame into the mix.

POSITION UPDATES PER SECONDELEMENTS, 1,000 TO 4,000120FPS (MAX)CSS68FPSMAIN THREAD32FPS
Translate with paint, busy main thread (Chrome)

We can see the CSS animation behaves the same as before, but the main thread animation is capped to under 40fps right off the bat, simply because it's sharing the CPU.

The catch with hardware accelerated animations is only certain operations like fade (opacity), move (transform) and a few others can be animated entirely by the GPU. Everything else needs a new paint, triggered by the main thread.

Therefore, if we're additionally animating a value that needs a repaint every frame, like box-shadow, we're needing to repaint this layer every frame, which needs a new snapshot, and a new GPU upload.

This is considerably slower than just animating translate, which would happen entirely on the GPU, or just animating box-shadow, which would happen on the main thread, without the memory and processing overhead of a compositor layer.

We can prove this with another test. Here, we're animating translate and box-shadow both together and separately, with and without layers.

FRAMES PER SECONDELEMENTS, 1 TO 50060FPS (MAX)TRANSLATE60FPSBOX-SHADOW47FPSBOTH (NO LAYER)47FPSBOTH, CSS (SEPARATE)12FPSBOTH, CSS (COMBINED)6FPS
Animating translate + box-shadow (Chrome low-end)

As expected, translate performs the best alone as this is fully hardware accelerated. Followed closely by box-shadow, which is triggering paint. But look at what happens when we animate both values together.

Both (combined) is a single CSS @keyframes animation for both values.

/* Both (combined) */
@keyframes both {
  to {
    transform: translateX(200px);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  }
}

Both (separate) defines a @keyframes animation for each value.

/* Both (separate) */
@keyframes move {
  to {
    transform: translateX(200px);
  }
}

@keyframes shadow {
  to {
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  }
}

Separating the animations performs twice as well as combining them, which is already interesting. Probably because combined, Chrome has to animate transform on the main thread alongside box-shadow whereas when separated some of the animation work is offloaded to the GPU.

Crucially, both of these CSS transform animations automatically make a layer for the element. Whereas the JavaScript animation, Both (no layer), actually animates with the same performance as the stand-alone CSS box-shadow animation.

Which brings us neatly onto our second surprise...

Surprise! translate3d isn't will-change

When the browser decides to create a layer is itself a matter of some complexity, but as we've just seen elements become layers if they animate transform via a browser API like Web Animations API, @keyframes or transition.

But, animations via requestAnimationFrame setting element.style every frame are opaque to the browser, so no layer is automatically created. Out the box, these animations trigger paint.

This is why it's always recommended to set will-change on these layers. Although we're still driving the animation logic from the main thread, we can at least still render these elements entirely via the compositor, the GPU, without paint.

If, like me, you're really old. Like depressingly old. Not old by life standards, but old by industry standards. Like Frontpage and Dreamweaver old. Flash old. Kids old. Knees old. If you're that old then you might still be in the habit of forcing elements to layers using a 3D transform like translateZ(0) or similar.

Until recently my mental model of setting a 3D transform was that it was essentially the same as will-change but without the "please".

But some time in the last couple years I noticed this wasn't delivering the same performance benefits as it used to, and after making Performance Lab I've found another behaviour that I'd never have imagined.

This time our test is a Motion layout animation. We animate a few large elements between two layouts, Motion moving each with a transform set from the main thread.

For the first run I wrote transform three ways:

  • translate: a 2D translate() with no layer.
  • translate3d: 3D transform triggering a layer.
  • will-change: the 2D translate(), with will-change: transform.

This time, we're going to look at GPU time (lower is better):

GPU TIMEELEMENTSTRANSLATE9XTRANSLATE3D8XWILL-CHANGE1X
Main thread animation performance (Chrome)

We can see that there's a big split in the workloads here. As expected, translate is the slowest because the element isn't a layer, so it's triggering paint every frame. The rasterisation work of a paint happens on the GPU which is showing here as 9x more work than when setting will-change.

translate3d is the surprise. It has a layer because setting a 3D transform creates a layer. The whole point of the layer is that the element gets painted once and then doesn't paint during the animation. And yet, in terms of GPU workload, it's sitting right next to the line with no layer at all. It's essentially doing nothing to improve performance.

What's going on?

Sub-pixel rendering

When we transform a layer, it can become blurry (another trade-off to bear in mind/context).

An easy example is scale. If we have a low resolution image and give it a big scale like 3 then it will look blurry, because we've taken a low-resolution image/layer and blown it up bigger than it was painted.

SCALE 1SCALE 3
Scaling a layer

As displays have improved, they've gone from rendering a single pixel with, well, a single pixel, to now rendering with a grid of 2x2 pixels, or more, or even fractions of a display pixel. These pixels-within-pixels are called "subpixels".

Although these smaller pixels are often not available to us programmatically, for instance when defining a layout (can't set width: 0.5px), the browser does use them to render content sharply.

Text is a big beneficiary of this, subpixels keep curves crisp. What it means though, is if you translate a character (especially a thin one like "l") 0.5px to the right when it was rendered for a specific subpixel arrangement, it will blur quite dramatically.

Therefore, when Chrome translates a layer by a subpixel, which in an animation is happening essentially every frame, it repaints the layer to keep it perfectly sharp.

Which raises the question, what happens if we animate with translateZ(0) or translate3d(x, y, 0) and keep the x/y pixels rounded?

GPU TIMEELEMENTSTRANSLATE9XTRANSLATE3D8XTRANSLATE3D(ROUNDED)1XWILL-CHANGE1X
Rounded translate3d (Chrome)

With rounded pixels you can see that the old 3D transform hack works as we'd expect again and the resultant performance profile is exactly the same as will-change.

Of course, this isn't practical. Rounded pixel values are not subtle, it's easy for a human to notice the stepped movement. You really have to use will-change now to get smoother animations.

Low-end devices

On our low-end device we can more easily compare FPS, so this time higher is better:

FRAMES PER SECONDCARDS, 2 TO 2060FPS (MAX)WILL-CHANGE58FPSTRANSLATE3D(ROUNDED)16FPSTRANSLATE3D15FPSTRANSLATE12FPS
Main thread animation performance (low-end device)

Here, even rounded translate3d values trigger paint. Why? Well, we suspect it's subpixel rendering again, as our device has a subpixel ratio of 2.8(!) meaning even rounded pixels are fractional. This shows the importance of running benchmarks across a variety of physical devices.

So yes, translate3d is not the same as will-change (at least no longer, at least in Chrome). Leave your millennial hacks at the door.

Surprise! Animation benchmarks are a rabbit hole

As I mentioned, I've previously steered away from over-benchmarking. Benchmarks are useful when they reveal very obvious architectural problems, like style or layout thrashing, or blocked main threads.

More often though, I've noticed these things by analysing output from the Performance tab in Chrome rather than raw numbers.

Reason being, manually running tests on my MacBook feels like a hall of mirrors. How many runs is enough? Why are results seeming to flip? Why do differences appear and disappear between runs?

This is why our Performance Doctor agent skill rates performance based on first principles rather than measured performance. It takes a dedicated, calibrated rig and someone with statistical understanding (Dr Perry) to do the latter properly.

Variance

All the numbers used for this article are medians, and medians don't really tell the other story, that of variance.

Basically, variance is the consistency of results.

We could run a test three times in a row and the variance between these runs might be very low. But then we come back for a new session tomorrow, with the exact same test, and today's session is different from yesterdays.

For example, our low-power device is a phone. At first, when variance between sessions was high, my first thought was overheating. But actually it was the opposite. Cold, the phone was 40% slower. For consistent results we first needed to warm the phone up.

Of course, people use cold phones. It could be the case that cutting out these results hides other performance characteristics that are also interesting.

On top of that, this specific finding could be different with a different phone, so as we add more devices we're collating our learnings into a calibration method that we can repeat for each.

Variance can come from anywhere. The GPU and CPU themselves each have a different variance. For one of our benchmarks, we found that the difference between the fastest and slowest run was very different for main thread animations and CSS animations:

750 ELEMENTSMAIN THREAD0.5 FPSCSS9.4 FPS2,000 ELEMENTSMAIN THREAD2.8 FPSCSS13.2 FPSBEST RUN MINUS WORST RUN, FPS
Spread between five runs (low-end device)

Because different animation libraries prioritise different workloads, this can mean some libraries are represented as "noisier" because they rely more on the GPU than CPU. Again, a factor to control.

There's one more source of variance, us. The way you measure an animation can change its performance, and more so on the CPU than GPU, seemingly dialing up the performance contrast when it's less pronounced in real life.

We've tried to design around all of this. Several runs, several sessions, several devices, custom browser builds, deciding on headed vs headless, no action or calling a "winner" until clear statistical significance, and Dr Perry always checking the statistical methodology.

Even after all this we're still discovering new sources of variance, and it's also why when we start publishing our benchmark suite next week, variance will be part of the reported numbers.

The one thing we promise to never do is cherry-pick. Every benchmark we write, we'll publish. Because if we start showing just the positive side to what we can see are real trade-offs and choices, these benchmarks are just marketing. You'll know they ain't jack.

Beyond hardware acceleration

Practically, our initial findings from the Performance Lab are being folded back into Motion as adaptive acceleration.

Here's what that looks like practically, on our low-end phone, with every element on screen animating transform and box-shadow again:

FRAMES PER SECONDELEMENTS, 50 TO 50060FPS (MAX)MOTION 1560FPSMOTION 1423FPSCSS21FPS
Translate with paint (low-end device)

Adaptive acceleration means defaulting to hardware acceleration and GPU rendering where possible, but also being mindful of when to adjust our layerisation strategy and rely more on the main thread in situations where it will result in higher frame rates on more devices.

The exact heuristics behind it will be in flux as we continue to run experiments and investigate, but one thing I'm trying to avoid is overfitting for stress tests of thousands of elements.

We ran an audit on hundreds of popular Framer websites and found that it was very rare that more than 11 elements animated at one time.

That said, when you're building an animation library for everyone, it is also important to bear in mind the extreme cases. So we're trying to find a balance.

And one more thing: Adaptive acceleration comes at a smaller file size for you, out of the box. Layout animations and drag gestures now load on-demand, asynchronously, for a ~30% smaller bundle if you never use them and faster page hydration if you do.

Motion 15 is out today.