RELEVANT PRODUCTS:
  • Optimizely X Web Experimentation
  • Optimizely X Web Personalization
  • Optimizely X Web Recommendations
  • Optimizely Classic

THIS ARTICLE WILL HELP YOU:
  • Decide when to use synchronous or asynchronous loading in the Optimizely snippet

A/B testing scripts can be loaded in two ways:

  • Synchronously, where scripts are loaded sequentially, one after another, starting with the <head> tag
  • Asynchronously, where some scripts can be loaded simultaneously

Loading a script asynchronously has the advantage of not slowing the page down, but it can cause a "flicker" on the page, where the original page loads, followed shortly by the variation page.

Optimizely uses a synchronous snippet to prevent flickering and a balanced content delivery network (CDN) system to make sure the impact on the page is minimized.

Do you just want to learn how to implement the Optimizely Snippet? Go to our article on implementing the Optimizely Snippet before you read this one.

You can also learn about custom snippets in Optimizely X Web or how to load variation code asynchronously within in a synchronous snippet.

If you specifically want to know about asynchronous and synchronous implementation, read on.

Synchronous and asynchronous loading types explained

A web page consists of a head and a body. Everything in the body is rendered by the browser while the head is used to load external resources (such as scripts and style sheets) and to add meta data to the page. When the page loads in a browser, the browser starts reading the html from top to bottom. The head section has a special characteristic: normally, the browser will not show anything (a white screen) until all the external resources are fully loaded. This is called "synchronous loading."

However, you could override this functionality to make certain elements load without waiting for all external resources to load. This is known as "asynchronous loading."

Here's what a page's <head> tag would look like when all resources are loaded in a synchronous manner:

<html> 
    <head> 
        <script src="1.js" /> 
        <script src="2.js" /> 
        <script src="3.js" /> 
        <link href="4.css" /> 
        <script src="5.js" /> 
    </head> 
    <body>Hello World!</body> 
</html>


When all resources in the head are synchronous, the elements will load in order, like this:


Here's what a page's <head> tag would look like when all resources are loaded in an asynchronous manner:

<html> 
    <head> 
        <script src="1.js" /> 
        <script async src="2.js" /> 
        <script src="3.js" /> 
        <link href="4.css" /> 
        <script src="5.js" /> 
    </head> 
    <body>Hello World!</body> 
</html>


When a script is asynchronous, it will load simultaneously with other scripts, like this:

You can see in the diagram, that scripts 2 and 3 are now able to load at the same time, which speeds up the overall loading of a page. However, the browser will not wait until the snippet is done loading before displaying the elements in the body to a visitor, which can lead to "flickering" (see below).

Synchronous loading

Synchronous loading helps minimize the loading time without the potential of a flicker on the page.

To make sure the impact on a website is as small as possible, the Optimizely script is hosted on first-class CDNs (Akamai and EdgeCast). CDNs are networks of servers that deliver resources like the Optimizely snippet to visitors. There are thousands of servers in a CDN, which are located around the globe to make sure that the Optimizely script is delivered as fast and reliable as possible wherever the end user might be located.

Optimizely has partnered with the best CDNs available and checks for every location which CDN is performing the best. We call this a balanced CDN system. You can read more about CDN balancing on our blog.

Asynchronous loading: strengths and drawbacks

On the plus side, loading asynchronously will prevent any delay in pageload because the page will attempt to load all elements simultaneously, including your A/B testing scripts.

However, when an A/B testing script is loaded much later than the page, a flicker of the page can occur. This flicker happens when the original page loads, followed by the variation. This is not desirable, especially when the loading of the A/B testing script is taking more time than is acceptable.

So in the asynchronous example above, a visitor might see "Hello world" in her browser before script 2 is done loading. If the A/B testing script is not loaded before the webpage is shown, it will result in a "flicker." Flickering means that the variation will load on the page shortly after the original page displays. This will make the outcome of your experiment less reliable and could compromise your visitor experience.