Anti-flicker snippets delay rendering

Anti-flicker snippets are used by client-side A/B testing libraries like Google Optimize, but they also delay rendering.

You might use an A/B testing library like Google Optimize, VWO or Optimizely. Most of the time, these are added to the site using client-side JavaScript. The page loads, the A/B testing JavaScript loads and then it sets it's experiment - maybe it changes your headline from "The Fastest Electric Car" to "Over 350 Mile Range on One Charge".

But there's a drawback to the client-side JavaScript version here: in some cases, the user might see the original headline, and then see that suddenly switch over to the new headline after the A/B testing JavaScript has loaded.

To get around that, A/B testing tools developed anti-flicker snippets. The Google Optimize code, for example, looks like this:

<style>.async-hide { opacity: 0 !important} </style>
<script>(function(a,s,y,n,c,h,i,d,e){s.className+=' '+y;h.start=1*new Date;
h.end=i=function(){s.className=s.className.replace(RegExp(' ?'+y),'')};

This works by adding opacity: 0 to the entire page - which shows the user a blank white screen - until either the test has loaded, or (in the example above) 4 seconds have passed, whichever is soonest.

It's worth noting that Google recommends a page needs to have a Largest Contentful Paint of 2.5 seconds or less in order to pass the Core Web Vitals - which is at odds with the anti-flicker snippet method of hiding the page for up to 4 seconds.

What does this look like?

Here's a side-by-side video (tested on a fast 3G connection) of Rightmove's website, which uses the Google Optimize anti-flicker snippet - compared with another version where I've removed the anti-flicker snippet.

It all happens very quickly, but the before version (with the anti-flicker snippet) starts rendering 5.1 seconds into the page load, while the after version starts rendering at 4.1 seconds. The anti-flicker snippet delayed rendering by a full second.

It can also have an impact on metrics like Largest Contentful Paint - the before version had an LCP of 5.1 seconds, and after removing the anti-flicker snippet, the LCP dropped to 4.5 seconds.

What should you do instead?

There's no simple answer to this - and there will have to be a compromise or trade-off somewhere. The ideal is to use a server-side A/B testing method instead - something that is supported by Google Optimize. The downside is that this is very complicated, and requires developer help.

Other options include at least reducing the timeout on the anti-flicker snippet from 4,000 ms to 2,500ms at most (given that LCP needs to occur within 2.5 seconds to be considered a pass by Google).

My personal preference is that the anti-flicker snippet is removed completely, although of course this can result in the visitor seeing the control switch to the experiment.

Andy Davies has written a really useful post on the impact that anti-flicker snippets can have on web performance, which you might find helpful when exploring other alternatives.

May 2021


Thanks for reading. If you found this interesting, you might also like Blackbird - site speed monitoring for ecommerce. Try it for free today.