DEV Community

Cover image for The Two Lines of CSS That Tanked Performance (120fps to 40fps)
Andrico
Andrico

Posted on • Originally published at component-odyssey.com

The Two Lines of CSS That Tanked Performance (120fps to 40fps)

I recently released Learn WCs and If you’ve seen it, you’ve likely noticed the animation in the background, where the coloured circles move diagonally across the screen. It looks like this:

It works nicely on Chrome and Safari, but I noticed a severe drop in performance on Firefox.

The performance was so bad, that I straight up disabled this animation in Firefox.

How does the animation work?

The animation is built using two nested divs. The outer div is the first child of the site’s body tag.

<body>
    <div class="background-mask">
        <div class="background-gradient"></div>
    </div>

    <!-- Rest of content -->
</body>
Enter fullscreen mode Exit fullscreen mode

The .background-gradient element is responsible for creating a gradient that spans the entire width and height of its parent container. Like so:

The background with no mask is just a gradient that goes from purple to red to orange.

The outer .background-mask is responsible for two things:

  1. It sets the position to fixed, and makes the container fill the entire dimensions of the viewport.
  2. Creates a dotted mask over the gradient

This ensures that the colour of the dots is the colour of the gradient directly underneath it:

The background with mask.

Here’s the CSS for everything I described above:

.background-mask {
    --mask-size: 24px;

    /* Position Styles */
    position: fixed;
    width: 100%;
    height: 100%;
    z-index: -1;

    /* Mask Styles */
    mask-image: radial-gradient(black 2px, transparent 2px);
    mask-size: var(--mask-size) var(--mask-size);
    mask-position: 0px 0px;
    animation: mask-move 3s infinite linear;
}

.background-gradient {
    background: var(--red);
    background-image: var(--gradient);
    width: 100%;
    height: 100%;
}

@keyframes mask-move {
    0% {
        mask-position: 0px 0px;
    }

    100% {
        mask-position: var(--mask-size) var(--mask-size);
    }
}

@media (prefers-reduced-motion: reduce) {
    .hero-background-mask {
        animation: none;
    }
}
Enter fullscreen mode Exit fullscreen mode

If you’re interested in learning more about masks in CSS, then I can recommend this comprehensive post by Ahmad Shadeed

What’s causing this drop in performance?

Not all CSS properties animate equally. Without going too much into how the browser renders HTML to the page (though I’ve outlined it here), there are a handful of stages it goes through. The three stages that we’re interested in are:

  • Layout - When the browser calculates the size and positions of the elements on the page
  • Paint - Draws all the visual aspects of the page, like images, colors, shadows, etc
  • Composite - Layering the elements on top of one another in the correct order

The order of the pipeline looks like this:

Layout → Paint → Composite

The layout and paint processes can be CPU-intensive, so it’s important to try and reduce the amount of times your CSS triggers the stages in the pipeline*.* The browser helps in some part by optimising performance for certain properties, some skip entire stages of the rendering pipeline and others can leverage hardware acceleration to move computation from the CPU to the GPU.

Animating certain properties, like translate and opacity , both avoids triggering a layout and uses hardware acceleration.

Sadly, this is not the case when animating mask-position. I took a look at Chrome and saw that the paint count for the background div was increasing on every frame. After a few seconds it had already triggered a paint over 1,000 times.

Paint count for the background div layer is 7941.

Even with this high paint count, the animation on Chrome feels smooth. However, it feels super janky on Firefox. Annoyingly, I couldn‘t find a way to measure the paint count on Firefox, so any assumptions I make about Firefox’s poor performance is purely conjecture.

What I did notice is that the animation is fine for small devices, but gets worse as the size of the screen increases. My working theory is that Firefox doesn’t batch the layout triggers for each the 24x24 masks, which causes the FPS to tank when more 24x24 masks are present. Again, I might be completely wrong here.

How did I fix this?

Instead of animating badly optimised CSS properties like mask-position , I needed to lean on the more performant properties, like translate.

The solution wasn’t to move the masks by 24px, but to instead move the entire background element using the translate property.

From an abstract standpoint, this is how the animation looks:

Here’s the two line change in the CSS:

/* --mask-size = 24px */

@keyframes mask-move {
    0% {
        transform: translate(calc(var(--mask-size) * -1), calc(var(--mask-size) * -1));
    }

    100% {
        transform: translate(0px, 0px);
    }
}
Enter fullscreen mode Exit fullscreen mode

The browser no longer animates the mask-position, which triggered a layout on each frame. Even though the background moves on each frame, through translate it doesn’t trigger a layout or a paint. You can see that the only paints twice, down from 1,000+ every minute.

Paint count for the background div layer is 2.

Eagle-eyed viewers will have spotted a problem. If you remember, the height and width of the background fills the viewport. Shifting the background left and up by 24px leaves us with this empty space in the viewport.

At the start of the animation the background doesn't cover the far right and far bottom of the viewport.

Solving it is as simple as adding the mask size to the width and height of the container:

.background-mask {
    --mask-size: 24px;

    width: calc(100% + var(--mask-size));
    height: calc(100% + var(--mask-size));
}
Enter fullscreen mode Exit fullscreen mode

Let’s take a look again in Firefox:

It may not be a perfect solution, but it’s always a little satisfying pulling off a fun smoke and mirrors CSS trick.

Top comments (0)