DEV Community

Cover image for Understanding async vs defer in the <script> Tag πŸš€
Ali Samir
Ali Samir

Posted on

Understanding async vs defer in the <script> Tag πŸš€

When working with scripts in HTML, you may have noticed the async and defer attributes on the <script> tag. These attributes play a significant role in optimizing the loading and execution of JavaScript files.

Understanding their differences and when to use each can improve your website’s performance and user experience.

In this article, we’ll explore how async and defer work and provide practical examples to clarify their usage.


πŸ“Œ The Problem with Default Script Loading

By default, when the browser encounters a <script> tag, it pauses parsing the HTML document to download and execute the script. This blocking behavior can delay the loading of the rest of the page, negatively affecting performance.

To address this, the async and defer attributes allow the non-blocking loading of scripts, enabling the browser to continue parsing HTML while the script is being downloaded.

However, these attributes differ in how they handle script execution.


πŸ“ What is async?

The async attribute loads the script asynchronously. This means:

  • The script is downloaded in parallel with the HTML parsing.

  • Once the script is downloaded, it is executed immediately, potentially interrupting the HTML parsing process.


Key Characteristics of async πŸ”»

1- Execution Order: Scripts with async are executed as soon as they are ready, regardless of their order in the document.

2- Best for Independent Scripts: Use async for scripts that don’t rely on or interact with other scripts or the DOM (e.g., analytics or ads).


✨ Example of async

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Async Example</title>
</head>
<body>
  <h1>Async vs Defer</h1>
  <script async src="script1.js"></script>
  <script async src="script2.js"></script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

In this example, script1.js and script2.js are downloaded and executed independently. Their execution order depends on which script finishes downloading first.



πŸ“ What is defer?

The defer attribute ensures that the script is downloaded in parallel but delays execution until after the HTML document is fully parsed.


Key Characteristics of defer πŸ”»

1- Execution Order: Scripts with defer are executed in the order they appear in the document.

2- Best for Dependent Scripts: Use defer for scripts that manipulate the DOM or depend on other scripts.


✨ Example of defer

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Defer Example</title>
  <script defer src="script1.js"></script>
  <script defer src="script2.js"></script>
</head>
<body>
  <h1>Async vs Defer</h1>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

In this example, script1.js is guaranteed to execute before script2.js, even though both scripts are downloaded simultaneously.


Comparing async and defer πŸš€

Attribute Loading Behavior Execution Timing Use Case
async Downloads in parallel Executes as soon as ready Independent scripts (e.g., analytics)
defer Downloads in parallel Executes after HTML parsing DOM-dependent scripts or multi-scripts

Visualizing the Difference πŸ’―

  • Without Attributes: Blocks HTML parsing until the script is downloaded and executed.

  • With async: HTML parsing continues during download but stops for execution.

  • With defer: HTML parsing and downloading happen simultaneously; execution waits until parsing is complete.


Practical Scenarios πŸ”»

Scenario 1: Loading Analytics

For analytics or tracking scripts that don’t interact with other scripts or the DOM:

<script async src="analytics.js"></script>
Enter fullscreen mode Exit fullscreen mode

Scenario 2: Loading Multiple Scripts with Dependencies

For scripts that rely on each other or manipulate the DOM:

<script defer src="library.js"></script>
<script defer src="main.js"></script>
Enter fullscreen mode Exit fullscreen mode

Here, library.js will load and execute before main.js, ensuring proper dependency handling.



Key Takeaways βœ…

  • Use async for independent scripts to load them quickly without waiting for HTML parsing.

  • Use defer for scripts that interact with the DOM or depend on other scripts.

  • Avoid using both async and defer on the same script as it may lead to unpredictable behavior.

By strategically using async and defer, you can optimize script loading, improve page speed, and enhance user experience.



🌐 Connect With Me On:

πŸ“ LinkedIn
πŸ“ X (Twitter)
πŸ“ Telegram
πŸ“ Instagram

Top comments (0)