DEV Community

Cover image for My home page was running 887 animations at once on phones. The main fix was one line.
Femi Akinyemi
Femi Akinyemi

Posted on

My home page was running 887 animations at once on phones. The main fix was one line.

I build speed tools for a living. So this one stings a little.

My product, Tilzix, turns a website into carousels for Instagram, TikTok and LinkedIn. The home page shows off the styles with real slides, drawn live in the browser, and most of them move. Text slides in, shapes float, one style has a row of little bulbs that blink like a theatre sign.

On my laptop it looked great. On iPhones, the page kept reloading, then gave up with "A problem repeatedly occurred".

Counting what was running

The first question was simple: how much is actually moving? The browser can tell you. Paste this into the console on any page:

document.getAnimations().filter(a => a.playState === "running").length
Enter fullscreen mode Exit fullscreen mode

That counts every CSS animation and transition currently running.

On my home page, after scrolling to the bottom on a phone-sized screen: 887.

Almost all of it was slides I had already scrolled past, still animating where nobody could see them.

The bug

Each sample slide only mounted when it came near the screen. That's the usual lazy pattern, and I thought it was enough:

const io = new IntersectionObserver(([e]) => {
  if (e.isIntersecting) {
    setNear(true);
    io.disconnect();
  }
}, { rootMargin: "100px" });
Enter fullscreen mode Exit fullscreen mode

See the problem? It mounts once and never lets go. Scroll down the page and every slide you pass gets drawn, and then stays drawn, animating forever. The page only ever gets heavier.

Lazy loading saves the first paint. It does nothing for the hundredth slide you scrolled past a minute ago.

The fix

Let the slide go when it leaves the screen:

const io = new IntersectionObserver(
  ([e]) => setNear(e.isIntersecting),
  { rootMargin: "40px 0px" }
);
Enter fullscreen mode Exit fullscreen mode
{near && <Slide ... />}
Enter fullscreen mode Exit fullscreen mode

Now a slide is only in the page while it's on screen or just about to be. Scroll past and it's removed, along with every animation inside it. Scroll back and it plays its entrance again, which looks deliberate rather than broken.

I also tightened the margin from 100px to 40px, so fewer slides wait just off screen.

The other culprit

One style on its own accounted for about 570 of those animations: the theatre-sign style, where every bulb is its own blinking animation. One slide in that style was on the home page, in the section about sizes.

It's a fine style when you're looking at one slide. On a busy home page it was a lot of work for one piece. I swapped that section to a lighter style.

The result

  • At any moment while scrolling: about 9 running animations, down from 887
  • At the busiest point, with the style gallery on screen: about 240
  • The iPhone reloads stopped

What I'm taking from it

  1. Count, don't guess. document.getAnimations() is one line. Run it at the bottom of your longest page.
  2. Mounting lazily isn't enough. Unmount too. If it scrolled away, it shouldn't still be running.
  3. Test on a real phone. My laptop never showed a thing.

I now run a script before every deploy that opens each page on a phone-sized screen, scrolls to the bottom and prints this count, so a page that creeps back up gets caught before it goes live.

If you try the one-liner on your own site, I'd like to know what number you get.

Top comments (0)