afyonkarahisarkitapfuari.com

Mastering JavaScript: Async vs. Defer for Optimal Performance

Written on

Understanding JavaScript Loading Techniques

When incorporating JavaScript into an HTML document, it's crucial to consider how it impacts page loading performance. The speed at which a webpage loads is significantly influenced by the placement and method of script insertion.

In this discussion, we will explore how the async and defer attributes can be utilized to manage script execution order and enhance page load efficiency.

Stay Updated with Web Development Trends!

Today’s browsers universally support the async and defer attributes for scripts. These attributes inform the browser that it can continue loading the page while the scripts are being fetched.

How the `<script>` Tag Functions Without Attributes

When a script tag lacks any attributes, the browser parses the HTML until it encounters the script. At that moment, parsing halts, and a request is initiated to retrieve the external script. The execution of the script must complete before the browser resumes parsing.

Using the `async` Attribute

By incorporating the async attribute, the browser downloads the script during HTML parsing. Once the download completes, the execution occurs immediately, potentially interrupting the parsing process:

<script src="directory/script1.js" async></script>

<script src="directory/script2.js" async></script>

Scripts marked as async run asynchronously, meaning they execute as soon as they finish downloading, without holding up the browser. Note that, in this scenario, script 2 may run before script 1.

Implementing the `defer` Attribute

In contrast, when the defer attribute is present, the browser still fetches the script while parsing the HTML, but it delays execution until after the entire document has been parsed. Deferred scripts are executed in the exact order they appear in the document:

<script src="directory/script1.js" defer></script>

<script src="directory/script2.js" defer></script>

Unlike async, defer ensures that scripts only run once the document is completely loaded.

Are you prepared to transform your development skills into a freelancing career? Whether you are just starting out or looking to enhance your skills, my Complete Guide to Freelancing can provide the essential insights you need!

The Complete Guide to Freelancing

Ready to be your own boss? Start today! This guide covers everything necessary to establish a successful freelancing venture from scratch.

Choosing Between Async and Defer

When deciding which attribute to use, consider the following guidelines:

  • Use async for modular scripts that do not depend on others.
  • Opt for defer for scripts that rely on or are depended upon by other scripts.
  • For small scripts that are dependencies for async scripts, consider using inline scripts placed above the async scripts.

The modern approach recommends placing scripts in the <head> section along with async or defer attributes. This strategy allows for fast script downloads without blocking the browser.

I hope this article has been helpful! Feel free to connect with me on Medium or Twitter, and don’t hesitate to leave any questions in the comments. I’m here to assist!

A little about me...

Hello! I’m Tim, a freelance business owner, web developer, and author. I help both novice and seasoned freelancers build sustainable and thriving businesses. For more articles, check out my blog.

Thank you for reading! 😊

In this first video, you'll discover various strategies for loading JavaScript, focusing on the async and defer attributes, which can significantly enhance your web performance.

The second video dives deeper into how to effectively utilize the async and defer attributes to optimize script loading and execution order in your projects.

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

Mastering Mental Math: How to Multiply Any Number by 11 Effortlessly

Discover a simple technique to multiply any number by 11 in your head using addition.

Strategies to Overcome Bad Habits: Understanding the Challenge

Discover key reasons why breaking bad habits is tough and effective strategies to succeed.

Unlocking the Secrets to Being the Sharpest Mind in Meetings

Discover key strategies to stand out and be perceived as the most intelligent person in any meeting with minimal effort.