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>
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>
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>
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>
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
anddefer
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)