DEV Community

Cover image for Understanding async and defer
Sanjeev Sharma
Sanjeev Sharma

Posted on • Originally published at frontendcamp.in

Understanding async and defer

This is one of the popular frontend interview questions. It tests interviewees knowledge on HTML, JS and Performance.

This is question #1 of Frontend Interview Questions series. If you're looking to level up your preparation or stay updated in general, consider signing up on FrontendCamp.


The script tag is used to add JavaScript to an HTML page. It could be an inline script or an external script.

<body>
  <!-- Some code before it -->

  <script>
    console.log("This is an inline script.");
  </script>

  <script src="https://example.com/external-script.js" />

  <!-- Some code after it -->
</body>
Enter fullscreen mode Exit fullscreen mode

While parsing the HTML, if browser encounters a script tag it will stop HTML parsing and start executing the JS script. If it's inline it will start with execution straight away but if it's an external script, it will be downloaded and then executed.

During this time, when JS script is being downloaded and executed, HTML parsing is blocked. It can only resume once the browser is done with executing the JS script.

Do you see what's wrong here? This will cause performance issues for the end user. If we have a lot of scripts or any script takes a lot of time to execute, user won't see the content of the page for a long time.

To solve exactly this, we have two attributes: async and defer.

async

If the async attribute is present, the script will be downloaded in parallel to parsing HTML and executed as soon as it is available.

If multiple scripts use the async attribute, the order of execution might be different than the order in which they appear in the HTML. The script that is available first will be executed first.

<body>
  <!-- Some code before it -->

  <script async src="https://example.com/external-script.js" />

  <!-- Some code after it -->
</body>
Enter fullscreen mode Exit fullscreen mode

defer

If the defer attribute is present, the script will be downloaded in parallel to HTML parsing(just like async) but executed after HTML parsing is finished and before firing DOMContentLoaded.

If multiple scripts use the defer attribute, the order of execution will be maintained, unlike async.

<body>
  <!-- Some code before it -->

  <script defer src="https://example.com/external-script.js" />

  <!-- Some code after it -->
</body>
Enter fullscreen mode Exit fullscreen mode

Summary

  1. Both async and defer download script without blocking HTML parsing.
  2. async script will be executed as soon as it's available. It could potentially block HTML parsing.
  3. defer script will only be executed once HTML parsing is complete but before firing DOMContentLoaded.
  4. Use async, if the order of execution doesn't matter and the script doesn't modify the DOM.
  5. Use defer, if the order is execution is important or the script modifies the DOM.
  6. Also, note that these attributes don't work on inline scripts i.e. scripts without src attribute.
  7. If both async and defer are added, async takes precedence.

Resources

MDN: The script element
FrontendCamp

Top comments (8)

Collapse
 
roshan_khan_28 profile image
roshan khan

async and defer, i always have to google this , good explaination

Collapse
 
thesanjeevsharma profile image
Sanjeev Sharma

I'm glad you liked it.

Collapse
 
mkboris profile image
Makoge Boris

Great explanation

Collapse
 
thesanjeevsharma profile image
Sanjeev Sharma

Thank you :)

Collapse
 
elakkiya_nandhini_1583930 profile image
Elakkiya Nandhini

Short and crispπŸ‘πŸ‘

Collapse
 
thesanjeevsharma profile image
Sanjeev Sharma

πŸ™

Collapse
 
fsfahim profile image
Fahim Faisal

In short, great explanation πŸ‘Œ

Collapse
 
thesanjeevsharma profile image
Sanjeev Sharma

I'm glad you liked it.