DEV Community

Cover image for Understanding async and defer
Sanjeev Sharma
Sanjeev Sharma Subscriber

Posted on • Originally published at frontendcamp.in

89 4 3 3 5

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

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (10)

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
 
makogeboris profile image
Makoge Boris

Great explanation

Collapse
 
thesanjeevsharma profile image
Sanjeev Sharma

Thank you :)

Collapse
 
sahildahekar profile image
Sahil Dahekar

Simple and detailed explaination ! Loved it

Collapse
 
thesanjeevsharma profile image
Sanjeev Sharma

Glad you liked it. :)

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.

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay