惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
Lohrmann on Cybersecurity
Martin Fowler
Martin Fowler
Engineering at Meta
Engineering at Meta
腾讯CDC
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Microsoft Azure Blog
Microsoft Azure Blog
G
Google Developers Blog
TaoSecurity Blog
TaoSecurity Blog
博客园_首页
Vercel News
Vercel News
Hugging Face - Blog
Hugging Face - Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Last Week in AI
Last Week in AI
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
The Exploit Database - CXSecurity.com
量子位
Project Zero
Project Zero
A
Arctic Wolf
小众软件
小众软件
NISL@THU
NISL@THU
C
CERT Recently Published Vulnerability Notes
有赞技术团队
有赞技术团队
MongoDB | Blog
MongoDB | Blog
博客园 - 聂微东
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
N
News and Events Feed by Topic
宝玉的分享
宝玉的分享
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Troy Hunt's Blog
P
Privacy & Cybersecurity Law Blog
Security Latest
Security Latest
B
Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
D
DataBreaches.Net
Schneier on Security
Schneier on Security
The Hacker News
The Hacker News
K
Kaspersky official blog
C
Check Point Blog
Hacker News: Ask HN
Hacker News: Ask HN
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Webroot Blog
Webroot Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
人人都是产品经理
人人都是产品经理
AI
AI
Cisco Talos Blog
Cisco Talos Blog
MyScale Blog
MyScale Blog
Cloudbric
Cloudbric
B
Blog RSS Feed
S
Schneier on Security
P
Palo Alto Networks Blog

CSS Wizardry

Front-End’s Missing Metric: The TBT Window Meet Your Users Where They Are with Obs.js Better Browser Caching with No-Vary-Search font-family Doesn’t Fall Back the Way You Think What Is CSS Containment and How Can I Use It? When All You Can Do Is All or Nothing, Do Nothing Obs.js: Context-Aware Web Performance for Everyone Low- and Mid-Tier Mobile for the Real World (2025) The Fastest Site in the Tour de France Making Sense of the Performance Extensibility API Why Do We Have a Cache-Control Request Header? HTML Is Not a Programming Language… Build for the Web, Build on the Web, Build with the Web Licensing Code on CSS Wizardry A Layered Approach to Speculation Rules Designing (and Evolving) a New Web Performance Score Core Web Vitals Colours The Ultimate Contract Templates for Tech Consultants: Protect Your Business and Get Paid Optimising for High Latency Environments Cache Grab: How Much Are You Leaving on the Table? blocking=render: Why would you do that?! Correctly Configure (Pre) Connections The Three Cs: 🤝 Concatenate, 🗜️ Compress, 🗳️ Cache What Is the Maximum max-age? How to Clear Cache and Cookies on a Customer’s Device The Ultimate Low-Quality Image Placeholder Technique Core Web Vitals for Search Engine Optimisation: What Do We Need to Know? The HTTP/1-liness of HTTP/2 In Defence of DOM­Content­Loaded Site-Speed Topography Remapped Why Not document.write()? Critical CSS? Not So Fast! Measure What You Impact, Not What You Influence Optimising Largest Contentful Paint Measuring Web Performance in Mobile Safari Site-Speed Topography Speed Up Google Fonts Real-World Effectiveness of Brotli Performance Budgets, Pragmatically Lazy Pre-Browsing with Prefetch Making Cloud.typography Fast(er) Time to First Byte: What It Is and How to Improve It Self-Host Your Static Assets Tips for Technical Interviews Cache-Control for Civilians Bandwidth or Latency: When to Optimise for Which ITCSS × Skillshare What If? CSS and Network Performance The Three Types of Performance Testing Getting to Know a Legacy Codebase Image Inconsistencies: How and When Browsers Download Images Identifying, Auditing, and Discussing Third Parties My Digital Music Setup Measuring the Hard-to-Measure Finding Dead CSS The Fallacies of Distributed Computing (Applied to Front-End Performance) Ten Years Old Relative Requirements Airplanes and Ashtrays Performance and Resilience: Stress-Testing Third Parties Refactoring Tunnels Little Things I Like to Do with Git Writing Tidy Code Configuring Git and Vim Base64 Encoding & Performance, Part 2: Gathering Data Base64 Encoding & Performance, Part 1: What’s Up with Base64? Code Smells in CSS Revisited Typography for Developers Moving CSS Wizardry onto HTTPS and HTTP/2 Ack for CSS Developers A New Year, a New Focus Preparing Vim for Apple’s Touch Bar Choosing the Correct Average CSS Shorthand Syntax Considered an Anti-Pattern CSS Wizardry Newsletter Nesting Your BEM? Improving Perceived Performance with Multiple Background Images Continue Normalising Your CSS Pure CSS Content Filter Pragmatic, Practical, and Progressive Theming with Custom Properties Refactoring CSS: The Three I’s Speaker’s Checklist: Before and After Your Talk Improving Your CSS with Parker The Importance of !important: Forcing Immutability in CSS Mixins Better for Performance Managing Typography on Large Apps White October Events Workshop Partnership BEMIT: Taking the BEM Naming Convention a Step Further Travelling Like You Want to, When You Have To Contextual Styling: UI Components, Nesting, and Implementation Detail Subtleties with Self-Chained Classes Cyclomatic Complexity: Logic in CSS Immutable CSS Can CSS Be Too Modular? More Transparent UI Code with Namespaces When to use @extend; when to use a mixin The Specificity Graph CSS Wizardry Ltd.: Year 1 in review CSS Guidelines 2.0.0
Speeding Up Async Snippets
Harry Roberts · 2022-10-13 · via CSS Wizardry

Written by on CSS Wizardry.

Table of Contents

Independent writing is brought to you via my wonderful Supporters.

  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:

<script>
  var script = document.createElement('script');
  script.src = 'https://third-party.io/bundle.min.js';
  document.head.appendChild(script);
</script>

Here, we…

  1. create a <script> element…
  2. whose src attribute is https://third-party.io/bundle.min.js
  3. and append it to the <head>.

The first thing I find most surprising is that the majority of developers I encounter do not know how this works, what it does, or why we do it. Let’s start there.

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 <head>. The reason they give us this cumbersome snippet, and not a much more succinct <script src="">, is purely historical: async snippets are a legacy performance hack.

When requesting JavaScript files from the DOM, they can be either blocking or non-blocking. Generally speaking, blocking files are worse for performance, especially when hosted on someone else’s origin. Async snippets inject files dynamically so as to make them asynchronous, or non-blocking, and therefore faster.

But what is it about this snippet that actually makes the file asynchronous? There’s no async attribute in sight, and the code itself isn’t doing anything special: it’s just injecting a script that resolves to a regular, blocking <script> tag in the DOM:

  ...
  <script src="https://third-party.io/bundle.min.js"></script>
</head>

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 <script src="" defer>.

Legacy async Support

It wasn’t until 2015 (admittedly, that is seven years ago now…) that all browsers supported the async attribute. For all major browsers, that date was 2011—over ten years ago. So, in order to work around it, third party vendors employed async snippets. Async snippets are, at their most basic, a polyfill.

Nowadays, however, we should be going straight into using <script src="" async>. Unless you have to support browsers in the realm of IE9, Opera 12, or Opera Mini, you do not need an async snippet (unless you do…).

What’s Wrong With the Polyfill?

If the polyfill works, what’s the benefit of moving to the async attribute? Sure, using something more modern feels nicer, but if they’re functionally identical, is it better?

Well, unfortunately, this performance polyfill is bad for performance.

For all the resulting script is asynchronous, the <script> block that creates it is fully synchronous, which means that the discovery of the script is governed by any and all synchronous work that happens before it, whether that’s other synchronous JS, HTML, or even CSS. Effectively, we’ve hidden the file from the browser until the very last moment, which means we’re completely failing to take advantage of one of the browser’s most elegant internals… the Preload Scanner.

The Preload Scanner

All major browsers contain an inert, secondary parser called the Preload Scanner. It is the job of the Preload Scanner to look ahead of the primary parser and asynchronously download any subresources it may find: images, stylesheets, scripts, and more. It does this in parallel to the primary parser’s work parsing and constructing the DOM.

Because the Preload Scanner is inert, it doesn’t run any JavaScript. In fact, for the most part, it only really looks out for tokenisable src and href attributes defined later in the HTML. Because it doesn’t run any JavaScript, the Preload Scanner is unable to uncover the reference to the script contained within our async snippet. This leaves the script completely hidden from view and thus unable to be fetched in parallel with other resources. Take the following waterfall:

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:

<script>
  var script = document.createElement('script');
  script.src = 'https://third-party.io/bundle.min.js';
  document.head.appendChild(script);
</script>

…we can literally just swap this out for the following in the same location or later in your HTML:

<script src="https://third-party.io/bundle.min.js" async></script>

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:

<script>
  var user_id     = 'USR-135-6911-7';
  var experiments = true;
  var prod        = true;
  var script      = document.createElement('script');
  script.src      = 'https://third-party.io/bundle.min.js?user=' + user_id;
  document.head.appendChild(script);
</script>

…with this:

<script>
  var user_id     = 'USR-135-6911-7';
  var experiments = true;
  var prod        = true;
</script>
<script src="https://third-party.io/bundle.min.js?user=USR-135-6911-7" async></script>

This works because, even though the <script src="" async> is asynchronous, the <script> block before it is synchronous, and is therefore guaranteed to run first, correctly initialising the config variables.

async doesn’t mean run as soon as you’re ready, it means run as soon as you’re ready on or after you’ve been declared. Any synchronous work defined before an async script will always execute first.

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:

<script>
  var script = document.createElement('script');
  var url    = document.URL;
  script.src = 'https://third-party.io/bundle.min.js&URL=' + url;
  document.head.appendChild(script);
</script>

In this instance, the async snippet is less about working around a performance issue, and more about a dynamism issue. The only optimisation I would recommend here—if the third party is important enough—is to complement the snippet with a preconnect for the origin in question:

<link rel=preconnect href="https://third-party.io">

<script>
  var script = document.createElement('script');
  var url    = document.URL;
  script.src = 'https://third-party.io/bundle.min.js&URL=' + url;
  document.head.appendChild(script);
</script>

Injecting Into a Page You Don’t Control

The second most probable need for an async snippet is if you are a third party injecting a fourth party into someone else’s DOM. In this instance, the async snippet is less about working around a performance issue, and more about an access issue. There is no performance enhancement that I would recommend here. Never preconnect a fourth, fifth, sixth party.

Takeaways

There are two main things I would like people to get from this post:

  1. Specifically, that async snippets are almost always an anti-pattern. If you’re utilising them, try move yourself onto a new syntax. If you’re responsible for one, try updating your service and documentation to use a new syntax.
  2. Generally, don’t work against the grain. While it might feel like we’re doing the right thing, not knowing the bigger picture may leave us working against ourselves and actually making things worse.

Frequently Asked Questions

What is an async snippet?

Async snippets are a JavaScript design pattern largely employed by third parties to inject subsequent scripts in an asynchronous manner. Async snippets are a legacy implementation designed to sidestep a lack of support for the HTML async attribute for script elements. Compared to the async attribute, async snippets are considered an anti-pattern and should be avoided.