Speeding Up Async Snippets – Web Performance and Site Speed Consultant


Written by on CSS Wizardry.

Table of Contents
  1. What Is an Async Snippet?
  2. Legacy async Support
  3. What’s Wrong With the Polyfill?
    1. The Preload Scanner
  4. The New Syntax
  5. When We Can’t Avoid Async Snippets
    1. Dynamic Script Locations
    2. Injecting Into a Page You Don’t Control
  6. Takeaways

If you’ve been a web developer for any reasonable amount of time, you’ve more
likely than not come across an async snippet before. At its simplest, it looks
a little like this:


Table of Contents

What Is an Async Snippet?

Snippets like these are usually employed by third parties for you to copy/paste into your HTML—usually, though not always, into the . The reason they give us this cumbersome snippet, and not a much more succinct "https://third-party.io/bundle.min.js">

How is this any different to just loading the file normally? What have we done that makes this asynchronous? Where is the magic?!

Well, the answer is nothing. We didn’t do a thing. It’s the spec which dictates that any scripts injected dynamically should be treated as asynchronous. Simply by inserting the script with a script, we’ve automatically opted into a standard browser behaviour. That’s really the extent of the whole technique.

But that begs the question… can’t we just use the async attribute?

As a bit of additional trivia, this means that adding script.async="async" is redundant—don’t bother with that. Interestingly, adding script.defer=defer does work, but again, you don’t need an async snippet to achieve that result—just use a regular

Here we can clearly see that the browser doesn’t discover the reference to the script (3) until the moment it has finished dealing with the CSS (2). This is because synchronous CSS blocks the execution of any subsequent synchronous JS, and remember, our async snippet itself is fully synchronous. The vertical purple line is a performance.mark() which marks the point at which the script actually executed. We therefore see a complete lack of parallelisation, and an execution timestamp of 3,127ms. To read more about the Preload Scanner, head to Andy DaviesHow the Browser Pre-loader Makes Pages Load Faster.

The New Syntax

There are few different ways to rewrite your async snippets now. For the simplest case, for example:
"https://third-party.io/bundle.min.js" async>

These are functionally identical.

If you’re feeling nervous about completely replacing your async snippet, or the async snippet contains config variables, then you can replace this:

"https://third-party.io/bundle.min.js?user=USR-135-6911-7" async>

This works because, even though the

Now we can see the Preload Scanner in action: complete parallelisation of our requests, and a JS execution timestamp of 2,340ms. Interestingly, the script itself took 297ms longer to download with this newer syntax, but still executed 787ms sooner! This is the power of the Preload Scanner.

When We Can’t Avoid Async Snippets

There are a couple of times when we can’t avoid async snippets, and therefore can’t really speed them up.

Dynamic Script Locations

Most notably would be when the URL for the script itself needs to be dynamic, for example, if we needed to pass the current page’s URL into the filepath itself:



Share this content:

I am a passionate blogger with extensive experience in web design. As a seasoned YouTube SEO expert, I have helped numerous creators optimize their content for maximum visibility.

Leave a Comment