日本語

How async and defer Change LCP in Core Web Vitals

​​Core Web Vitals​Chrome,HTML,JavaScript,Playwright,web-vitals

I measured how adding async or defer to a <script src> in <head> changes the LCP (Largest Contentful Paint) Core Web Vital. Instead of a real slow network, I used a relay I wrote to delay and throttle traffic. For a script that takes a long time for the server to return, LCP without an attribute was 1,752ms, and with either async or defer it was 1,280ms, the same as a page without the script.

However, async and defer did produce different LCP results in some cases. When a script that takes time to execute was read on a page with a large HTML document, LCP with defer was 528ms slower than with async. On a page where the script inserts the hero image, async was 152ms faster. These results can help you decide between async and defer for measurement tags, ads, and widget scripts that Lighthouse flags as "render-blocking resources."

Note

The test environment was Ubuntu 24.04, Node.js 22.23.5, web-vitals 6.2.3, and Playwright 1.63.0. The browser was Chromium 158, which Playwright's chromium.launch() starts by default, and I launched a new browser for each run. The server was an HTTP/1.1 server written with Node.js's http module. In place of a slow network, I wrote and ran my own TCP relay that delays data and throttles bandwidth. I did not test type="module" scripts, inline scripts, the execution order of multiple scripts, scripts loaded from another domain, HTTP/2 or HTTP/3, browsers other than Chrome, or real-user data (field data).

The three markup options and the test pages

I compared three ways of writing the script in <head>, plus one option with no attribute placed just before </body>.

html
<script src="/app.js"></script>        <!-- no attribute -->
<script src="/app.js" async></script>  <!-- async -->
<script src="/app.js" defer></script>  <!-- defer -->

According to MDN's script element reference, a script with no attribute stops HTML parsing until it has been fetched and executed. async fetches the script in parallel with HTML parsing and executes it as soon as it arrives. defer also fetches in parallel, but executes after HTML parsing finishes and before the DOMContentLoaded event.

The page contained a heading, a full-width hero image (hero.jpg, 1200×675, 129,547 bytes), and a short paragraph, for a total HTML size of 407 bytes. For the large-HTML variant, I added paragraphs after the hero image to bring the HTML to 154,007 bytes. The script (app.js) was about 20KB. Depending on the query string, the server changed how long it waited before responding and how long the script blocked the main thread with computation when it ran.

The relay I used in place of a slow network delayed data by 75ms one way (150ms round trip) and limited downstream traffic from the server to the browser to 200,000 bytes per second (1.6 Mbps) across all connections combined. Fetching hero.jpg with curl through the relay took 0.96 seconds. For measurement, I opened the page with Playwright and recorded LCP and its breakdown using the attribution build of Google's web-vitals library. The viewport was 800×600, and I measured each option five times. In all 75 runs, hero.jpg was the LCP element.

A horizontal bar chart comparing LCP medians across four cases, by how the script is written. For a slow-arriving script, no attribute gave 1,752ms, while async, defer, and the end of body all gave 1,280ms. For a script that takes 800ms to run, with a small HTML, no script gave 1,280ms, no attribute 1,644ms, async 1,704ms, and defer 1,700ms. With a large HTML, no script gave 1,740ms, async 1,884ms, and defer 2,412ms (pink). On a page where the script inserts the hero image, no attribute gave 1,996ms, the end of body 2,172ms, async 1,840ms, and defer 1,992ms
LCP medians (five runs each). Gray is the page without a script. The largest gap between async and defer was for a heavy script read with a large HTML document

A slow-to-arrive script delayed LCP only without an attribute

I made the server wait 1,000ms before returning app.js, to simulate a slow response from a measurement tag or widget server. The script finished executing quickly.

Script in head LCP median FCP median DOMContentLoaded (1st run)
No script 1,280ms 492ms 320ms
No attribute 1,752ms 1,752ms 1,727ms
async 1,280ms 488ms 320ms
defer 1,280ms 492ms 1,725ms
No attribute, just before </body> 1,280ms 492ms 1,741ms

On the page without an attribute, nothing was displayed until app.js arrived at about 1,724ms, so FCP and LCP had the same value. The hero image fetch started at about 0.32 seconds, even though HTML parsing was blocked, and the image arrived at 1,267ms. As the web.dev preload scanner article explains, while parsing is blocked, the browser still scans ahead in the HTML and starts fetching <img> elements it finds. Even so, the time from the image arriving to being rendered (elementRenderDelay) was about 490ms, which added 472ms to LCP.

The pages with async and defer had the same LCP and FCP as the page without a script. The difference was in DOMContentLoaded: the defer page waited for the script to execute, pushing it to 1,725ms. The page with the script just before </body> also had an LCP of 1,280ms, because the hero image was parsed before the script.

The initial priority Chrome gave the request was High for the app.js with no attribute, and Low for async and defer. The web.dev Fetch Priority article also states that async and defer scripts get Low priority.

A heavy-to-execute script delayed LCP with every option

Next, I made app.js return immediately, but block the main thread for 800ms when it ran. The HTML stayed small.

Script in head LCP median FCP median
No script (same page as the previous section) 1,280ms 492ms
No attribute 1,644ms 1,644ms
async 1,704ms 492ms
defer 1,700ms 492ms

For the LCP median, async and defer were 56 to 60ms slower than no attribute. However, the five no-attribute runs ranged from 1,632ms to 1,716ms, and two of those five were slower than any async or defer run (1,700 to 1,704ms). So it is reasonable to read the LCP as essentially the same. In the first async run, app.js arrived at 889ms and executed from 890 to 1,690ms, and the hero image arrived at 1,371ms. The main thread was busy when the image arrived, so rendering waited until the script finished, and elementRenderDelay was about 330ms. The web.dev LCP optimization article also notes that long tasks that block the main thread can delay element rendering.

Because the HTML was small, the defer script also began executing around 890ms, the same as async: by the time it arrived, HTML parsing had already finished. In this case, there was no difference between async and defer. The clear difference was in FCP. Without an attribute, FCP was delayed to 1,644ms, significantly delaying the first paint.

With a large HTML, a heavy script was 528ms slower with defer

I read the same heavy script on a page with a 154,007-byte HTML document. In the browser, receiving the whole HTML took about 1.5 to 1.6 seconds.

Script in head LCP median app.js execution (1st run) Image arrival time (1st run) elementRenderDelay (1st run)
No script 1,740ms none 1,723ms 14ms
async 1,884ms 1,066 to 1,866ms 1,825ms 59ms
defer 2,412ms 1,594 to 2,394ms 1,825ms 583ms

With async, the script started executing at 1,066ms, as soon as it arrived, while the HTML was still being received. Execution finished 41ms after the hero image arrived, so the rendering delay was only 59ms. The page was 144ms slower than the page with no script. That came from this delay, plus the image transfer sharing the connection with app.js, which stretched it from 1,320ms to 1,425ms in the first run.

With defer, app.js arrived at 1,064ms, but execution waited until HTML reception finished at 1,587ms. The hero image arrived at 1,825ms, during execution, so rendering waited until execution finished at 2,394ms. When the image arrived, async had only 41ms of execution left, while defer had 569ms left, which made LCP 528ms slower than async.

Which one is faster depends on how the script's execution time overlaps with the time the image arrives. As in the small-HTML case in the previous section, if execution happens at the same time, there is no difference.

For a page where the script inserts the hero image, async was faster

Finally, I measured a page where the HTML contains only an empty <div id="app">, and app.js creates an <img> and inserts it. I used the larger HTML, 154,007 bytes. If #app does not exist when the script runs, app.js waits for DOMContentLoaded before inserting the image (the following code is an excerpt from app.js).

js
function show() {
  var img = document.createElement('img');
  img.src = '/hero.jpg'; img.alt = 'ヒーロー画像'; img.width = 1200; img.height = 675;
  document.getElementById('app').appendChild(img);
}
if (document.getElementById('app')) show(); else document.addEventListener('DOMContentLoaded', show);
Script placement LCP median FCP median Image request start (1st run)
In head, no attribute 1,996ms 972ms 1,185ms
No attribute, just before </body> 2,172ms 776ms 1,356ms
In head, async 1,840ms 784ms 905ms
In head, defer 1,992ms 772ms 1,181ms

The async script arrived at 902ms and executed while the HTML was still being parsed (document.readyState was loading). By then, parsing had reached #app, so the script inserted the <img> right away, and the image request started at 905ms. defer waits until HTML parsing finishes, at about 1,180ms, so the image request was delayed until then. The LCP difference was 152ms.

async was faster because #app had already been parsed before the script arrived. The no-attribute case in <head> shows what happens when the script runs before #app exists. Even though the script executed as early as 905ms, #app did not yet exist at that point, so the script waited for DOMContentLoaded before inserting the image. The image request started at 1,185ms, almost the same time as defer (1,181ms). In addition, nothing was displayed until the script arrived, so FCP was 972ms, about 200ms later than the others. The page with the script just before </body> had the latest LCP, because the <script> was at the very end of the HTML. The app.js request did not start until just after HTML reception finished at 1,077ms, at 1,079ms.

A <img> inserted by a script is not in the HTML, so the preload scanner cannot find it ahead of time (see the web.dev preload scanner article). In this test, the image request never started around 0.4 seconds as it did for an <img> written in the HTML.

Point

When the LCP element is written in the HTML, neither async nor defer let a slow-to-arrive script hold back LCP. The two differed only when script execution was heavy and when the script created the LCP element. In both cases, async, which starts executing sooner, had the faster LCP.

A script with no attribute in head is flagged as a render-blocking resource by Lighthouse

According to Chrome's Lighthouse article on eliminating render-blocking resources, a <script> in <head> without defer or async is flagged as a render-blocking resource. It recommends keeping code you do not need right away in an external file and adding async or defer to that <script>.

Which one to use also depends on what the script relies on. According to MDN, defer scripts run in the order they appear in the document, while async scripts run in the order they arrive, so the order is not guaranteed. For scripts that depend on execution order, you need to account for this difference when choosing. In this test, for a page with the LCP element written in the HTML and a light script, either option left LCP unchanged.

​Posted by 86