Scanfully

Understanding Async and Defer in Script Tags

JavaScript loading and execution can delay rendering and affect Core Web Vitals such as LCP, alongside other metrics such as FCP. Choose a loading strategy that fits the script’s dependencies and the page’s requirements.

The async and defer attributes control when an external script downloads and executes relative to HTML parsing.

Understanding Script Tags in HTML

Script tags (<script> anything that goes in between these two </script>) in the HTML are the placeholders that tell the web browser to execute or link to executable code, most commonly JavaScript.

Understanding Async and Defer in Script Tags

The Anatomy of Script Tags

A script element can contain inline JavaScript or use its src attribute to reference an external .js file.

Attributes: Tuning Performance

Script tags come equipped with various attributes allowing developers to fine-tune their behavior.

Common Attributes

  • src: Specifies the URL of an external script file.
  • type: Indicates the MIME type of the script; however, it’s less commonly used nowadays as text/javascript is the default type.
  • async: Download the script without blocking HTML parsing and execute it as soon as it’s available.
  • defer: Wait to execute the script until after the HTML document has been fully parsed.

Use async and defer to control script execution relative to HTML parsing.

Interaction with HTML Parsing

A classic external script without async or defer can pause HTML parsing while it downloads and executes. These attributes allow downloading alongside parsing, but their execution timing differs.

Async: Execute When the Download Finishes

The async attribute lets an external script download while the browser continues parsing HTML.

How Async Works

With async, the script executes when its download finishes. It does not wait for HTML parsing to finish.

Ideal Use Case for Async

Use Async for scripts that do not depend on other scripts or a fully parsed Document Object Model (DOM). Independent analytics scripts are a common use for async.

Defer: Execute After HTML Parsing

The defer attribute separates downloading the script from executing it after HTML parsing.

How Defer Works

The browser downloads a script marked defer in the background and executes it after HTML parsing finishes.

Ideal Use Case for Defer

Use Defer for scripts that need the parsed DOM. For example, a chatbot that attaches to elements in the page may need defer so those elements exist before it runs.

Scripts Without Async or Defer

For a classic external script without async or defer, the browser pauses HTML parsing while it downloads and executes the script.

Check Dependencies Before Changing Attributes

Choose async or defer according to the script’s dependencies and timing requirements.


Async and defer change execution timing; test the page’s functions after changing either attribute.

Rule of Thumb

Use Async for independent scripts and Defer for scripts that need parsed HTML. Check dependencies before changing the loading behavior.

After choosing async or defer, measure loading performance and confirm that the script still runs when the page needs it.

Enter Cloudflare

Cloudflare offers delivery features alongside async and defer. Review those features and test the result with your scripts; using async or defer still requires compatible execution timing.

Rocket Loader™

Rocket Loader™ changes how JavaScript loads on a page.

Rocket Loader™ changes script loading alongside the behavior set by async and defer. Test scripts using async and defer after enabling it, especially when other scripts depend on them.

Check Async and Defer Behavior

Check when each async script executes and whether each defer script can access the DOM elements it needs.

Auto Minify

Auto Minify reduces script file size by removing unnecessary whitespace and comments.

Auto Minify takes the excess whitespace and comments out of your JavaScript files. This reduction in file size means a quicker download time, lessening the performance impact whether you’re using async, defer, or neither.

Impact on Async and Defer

Minification reduces the data downloaded for both async and defer scripts. Measure the result rather than assuming smaller files will execute faster.

Cloudflare CDN

Cloudflare’s CDN caches your JavaScript files across its global network. This puts your scripts closer to your users, slashing the time it takes to download them.

Collaborating with Async and Defer

A CDN can reduce download latency for async scripts. Defer scripts can also download sooner while still waiting for HTML parsing to finish before execution.

Smart Edge Revalidation

Smart Edge Revalidation works by ensuring that only changed content gets downloaded again. If your visitor has an outdated cached version of a script, Cloudflare only fetches what’s new.

Compatibility with Async and Defer

Review cache revalidation separately from async and defer execution timing. Check whether the browser downloads a new script or reuses a cached response.

Test Cloudflare’s settings with the async and defer attributes used by your site.

Optimizing Scripts with Perfmatters

Perfmatters includes tools for controlling script loading alongside async and defer.

Script Manager

The Script Manager enables you to selectively disable scripts on a per-page or post basis. This fine-tuning capacity means you can prevent scripts with async or defer from loading where they’re not needed, thus reducing unnecessary requests and improving loading speed.

Fine-Tuning Async and Defer

Use the Script Manager to disable unneeded async scripts on selected pages. For defer scripts, check whether the page contains the elements they depend on before disabling them.

Delay JavaScript Execution

Perfmatters can delay JavaScript until a user’s first interaction, such as a click or scroll.

Interaction with Async and Defer

Delaying JavaScript until user interaction adds a condition beyond the timing set by async or defer. Test that required page functions remain available when visitors need them.

CDN Rewrite: Optimizing Delivery

Perfmatters makes it simple to integrate with your CDN through its URL rewriting feature. This feature rewrites the URLs of your static assets so they’re served from the CDN, reducing the distance data needs to travel and speeding up delivery.

Coordination with Async and Defer

CDN Rewrite changes the delivery URL for scripts using async or defer. After changing the source, check that Async scripts still run independently and defer scripts run after parsing.

Preloading Resources

Perfmatters lets you specify scripts and other resources to preload. Preloading tells the browser to request selected files early.

This can be particularly helpful for defer scripts that are crucial for page functionality.

How It Enhances Async and Defer

Check when a script is discovered and needed before preloading it. Choosing Async or defer alone does not establish whether preloading will improve the page.

Check the combined effect of Perfmatters settings and your async and defer attributes before keeping a change.


If a WordPress plugin controls script output, tools such as Cloudflare and Perfmatters may help adjust delivery. Test those changes alongside async and defer and verify the page still works.

Previous article
Next article

Find out what’s breaking today.

Connect your first site in minutes and let Scanfully keep watch.

$2.99 per site/month · 14-day free trial · No credit card needed