<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Jugesh Raghav]]></title><description><![CDATA[Jugesh Raghav]]></description><link>https://jugeshraghav.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Wed, 14 Oct 2026 02:59:37 GMT</lastBuildDate><atom:link href="https://jugeshraghav.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Async vs Defer]]></title><description><![CDATA[Ever wondered how to optimize your website's loading speed? JavaScript files are essential for interactivity, but they can also slow down page rendering. This is where async and defer come in - attributes you can add to script tags to control how Jav...]]></description><link>https://jugeshraghav.hashnode.dev/async-vs-defer</link><guid isPermaLink="true">https://jugeshraghav.hashnode.dev/async-vs-defer</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[asynchronous]]></category><category><![CDATA[async]]></category><category><![CDATA[defer]]></category><category><![CDATA[Programming Blogs]]></category><category><![CDATA[Programming Tips]]></category><dc:creator><![CDATA[jugesh raghav]]></dc:creator><pubDate>Mon, 10 Jun 2024 16:22:22 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1718030311478/a7e3e9f4-26d9-4bb2-99fb-576cc2b4b166.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Ever wondered how to optimize your website's loading speed? JavaScript files are essential for interactivity, but they can also slow down page rendering. This is where <code>async</code> and <code>defer</code> come in - attributes you can add to script tags to control how JavaScript loads. Let's dive into the ring and see how these two contenders differ!</p>
<p>Initially let's visualize how normal scripts are downloaded and how webpage is painted on the browser.</p>
<ol>
<li><p><strong>HTML Parsing:</strong> When you load a webpage, the browser starts by parsing the HTML code. It encounters the <code>&lt;script&gt;</code> tag and pauses the parsing process.</p>
</li>
<li><p><strong>Script Download:</strong> The browser then identifies the source of the script specified in the <code>src</code> attribute. It initiates a request to download the JavaScript file from the web server.</p>
</li>
<li><p><strong>Blocking the Parse:</strong> While the script is downloading, the browser typically halts parsing the HTML code. This is because the script might contain code that interacts with elements defined later in the HTML. By pausing, the browser ensures the script has access to all the necessary elements when it runs.</p>
</li>
<li><p><strong>Script Execution:</strong> Once the script is downloaded, the browser stops pausing and resumes parsing the HTML. It then executes the downloaded script code line by line.</p>
</li>
<li><p><strong>Continue Parsing:</strong> After executing the script, the browser continues parsing the remaining HTML, rendering the page content as it goes.</p>
</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1718035688833/87809249-72a0-46b8-8d2b-3d665d70107e.png" alt class="image--center mx-auto" /></p>
<p><strong>Why do we need Async and Defer?</strong></p>
<p>This can significantly slow down the perceived loading speed of your website, especially if the script is large or takes time to download.</p>
<p><strong>The Contenders: Async vs Defer</strong></p>
<p>Both <code>async</code> and <code>defer</code> allow the browser to download JavaScript files asynchronously, meaning it doesn't have to wait for the script to finish downloading before continuing to parse the HTML. This improves perceived performance for users. However, the key difference lies in <strong>when</strong> the script actually executes:</p>
<ul>
<li><p><strong>Async:</strong> Think of it as the impatient contender. An async script is downloaded asynchronously, but it can execute <strong>as soon as it's available</strong>, even if the HTML parsing isn't complete. This can lead to scripts running out of order or encountering errors if they rely on elements that haven't been loaded yet.</p>
</li>
<li><p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1718036151254/785d5dd3-2446-4a23-811f-a1ffe1e7bd01.png" alt class="image--center mx-auto" /></p>
<p>  <strong>Defer:</strong> This contender is more cautious. A defer script is also downloaded asynchronously, but its execution is <strong>deferred</strong> until <strong>after the HTML parsing is complete</strong>. This ensures the script runs in the order it appears in the HTML and has access to the fully loaded DOM (Document Object Model) for manipulation.</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1718036162068/a93b9077-2251-41e1-93ad-95b7a9419b8f.png" alt class="image--center mx-auto" /></p>
<p><strong>Choosing Your Champion: When to Use Async vs Defer</strong></p>
<p>So, which attribute should you use? Here's a breakdown to help you decide:</p>
<ul>
<li><p><strong>Use async for:</strong></p>
<ul>
<li><p>Scripts that are <strong>independent</strong> and don't rely on the DOM or other scripts.</p>
</li>
<li><p>Analytics trackers or third-party widgets that can load without affecting the core functionality of your page.</p>
</li>
</ul>
</li>
<li><p><strong>Use defer for:</strong></p>
<ul>
<li><p>Scripts that <strong>depend on the DOM</strong> being fully parsed to function correctly.</p>
</li>
<li><p>Scripts that need to <strong>run in a specific order</strong> relative to other scripts.</p>
</li>
<li><p>Most core JavaScript files that interact with the page content.</p>
</li>
</ul>
</li>
</ul>
<p><strong>Remember:</strong></p>
<ul>
<li><p><strong>Order matters:</strong> While defer ensures scripts run in the order they appear in the HTML, async scripts have no guaranteed order.</p>
</li>
<li><p><strong>Consider dependencies:</strong> If scripts rely on each other, using defer is generally safer.</p>
</li>
<li><p><strong>Test thoroughly:</strong> No matter which attribute you use, test your website across different browsers to ensure everything works as expected.</p>
</li>
</ul>
<p><strong>The Final Bell: Async and Defer Together</strong></p>
<p>In some cases, you might even use both <code>async</code> and <code>defer</code> strategically. For instance, you could use async for a critical, small script that needs to run ASAP, and defer for the remaining scripts.</p>
<p>By understanding <code>async</code> and <code>defer</code>, you can optimize your website's loading strategy and deliver a faster, smoother experience for your users. So, put on your developer hat, choose your champion wisely, and get ready to create a winning website!</p>
]]></content:encoded></item></channel></rss>