What is the difference between async and defer? Understanding download timing, execution timing, and order

ProcrustesPR

Both async and defer allow external scripts to download without blocking HTML parsing. The main differences are when they execute and whether execution order is preserved. An async script executes once its download completes, without guaranteeing the order specified in the markup. A defer script waits until HTML parsing is complete, then executes in markup order. The comparison below focuses on non-module external scripts referenced with src directly in HTML. It does not apply to all inline, module, or dynamically inserted scripts.

ProcrustesPRProcrustesPRDoes my website need a speed score of 100?

What they share: downloading does not have to pause HTML parsing

According to web.dev, when the HTML parser encounters a non-module external script without async or defer, it waits for the script to finish downloading, parsing, and executing before continuing with the remaining HTML. Modules differ because their execution is deferred by default. The parser waits for regular scripts because they may access or modify the DOM while it is still being built.

Adding async or defer removes the wait during the download, but a non-blocking download does not mean execution is entirely non-blocking. If an async script starts executing before HTML parsing is complete, it still interrupts parsing. So “asynchronous loading” does not mean that the script no longer affects how the browser processes the page.

The key difference: executing immediately or waiting to execute in order

  • async: The script is parsed and executed once its download completes. Multiple scripts may execute in a different order from the one specified in HTML, so their placement in the markup cannot guarantee execution order.
  • defer: Downloading can proceed alongside HTML parsing, but execution waits until HTML parsing is complete. Multiple defer scripts execute in markup order.

For example, suppose A is a library and B calls functionality provided by A. If both use async, placing A before B does not guarantee that A executes first. If both are defer scripts in the context described above, they execute in their markup order: A, then B. This is a difference in execution order, not a guarantee about download speed.

Likewise, defer postpones execution; it does not mean that downloads must wait until the user can see the page.

Before choosing, check how the script is used

Start by asking: Does the script need to wait until HTML parsing is complete? Does it depend on another script executing first? If either requirement applies, defer is a better match. Consider async if the script can execute independently and does not need to wait for HTML parsing to finish.

The loading method also matters. Scripts with type=”module” have deferred execution by default, including inline modules. Regular inline scripts, however, block HTML parsing while they are being parsed and executed. web.dev also notes that scripts loaded by dynamically inserting them into the DOM with JavaScript behave similarly to async; you cannot assume they have defer’s ordering guarantee.

After changing the attributes, test that the scripts still work—especially any functionality that relies on script dependencies. Simply checking that the attributes have been added is not enough.

ProcrustesPRProcrustesPRCan a One-Page Website Do SEO?

async and defer: perspectives from across the web

Do synchronous scripts prevent the entire page from rendering?

Not necessarily. While waiting for a parser-blocking synchronous script, the browser may still render HTML it has already received, but the parser cannot proceed past the script to process subsequent content. If a parser-blocking script is in the head, it may effectively prevent all page content from rendering. This is related to the script's ability to change the DOM or CSSOM.

Is async suitable for scripts with dependencies?

If dependencies require multiple scripts to execute in a specific order, you should not rely on async alone: they may execute out of markup order. One author favors async for content they want to use early or that has dependencies, but that recommendation does not replace checking execution order. For non-module external scripts referenced directly in HTML, consider defer if they need to execute in markup order.

What should you test after adding these attributes?

Thoroughly test whether the scripts and their associated functionality work correctly; simply confirming that the page opens is not enough. One source cautions that these attributes can cause problems for certain types of scripts. In particular, when functionality depends on script execution order, check that the adjusted order meets its requirements. The appropriate attribute still depends on how the script is used.

ProcrustesPREight SEO essentials for beginners: Website speed

References

  1. Optimize resource loadingweb.dev — Discusses: Is async suitable for scripts with dependencies?; What should you test after adding these attributes?
  2. Understand the critical pathweb.dev — Discusses: Do synchronous scripts prevent the entire page from rendering?
  3. Google PageSpeed Insights: What It Is & How to Boost Your Scorewww.semrush.com · 2023-07-10T09:40:00Z — Discusses: What should you test after adding these attributes?
  4. Core Web Vitals (核心頁面指標) 是什麼及如何優化-完整版tenten.co · 2022-06-30T16:51:16.000Z — Discusses: Is async suitable for scripts with dependencies?

Related articles

Does my website need a speed score of 100?Can a One-Page Website Do SEO?Eight SEO essentials for beginners: Website speedHow should you balance CNAME convenience and performance?How Should a CMS Handle Images? From Upload Optimization to Descriptions and URL ManagementWhat Are the Benefits of HSTS Preload?How does HTTPS affect indexing, security, and website speed?What are the benefits and development challenges of SPAs?
ProcrustesPR
ProcrustesPR