Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Sheng Hsu
Sheng Hsu

Posted on

Async vs Defer in JavaScript

In the world of web development, performance is key. One of the ways to improve the performance of your website is by controlling how your JavaScript files are loaded.

This is where theasync anddefer attributes come into play. These attributes, when used with the<script> tag, can significantly impact the loading and execution of your JavaScript files.

What does Async do?

Theasync attribute in JavaScript allows the browser to continue parsing the HTML document while a script is being downloaded.

This means that the script is executed as soon as it is available, even if the HTML is not fully parsed. This can speed up the loading time of your page, but it also means that scripts may be executed out of order if they are loaded at different speeds.

What does Defer do?

On the other hand, thedefer attribute tells the browser to continue parsing the HTML document while the script is being downloaded, but to wait until the HTML is fully parsed before executing the script.

This ensures that scripts are executed in the order they appear in the HTML document, which can be important if one script depends on another.

Which is better?

The choice betweenasync anddefer depends on the specific needs of your website. If your scripts are independent and do not rely on each other,async can be a good choice because it allows scripts to execute as soon as they are available.

However, if your scripts depend on each other,defer may be a better choice because it ensures that scripts are executed in the correct order.

What happens when you use Async and Defer together?

Technically, you can use bothasync anddefer attributes on the same<script> tag. However, this is not recommended.

The reason is thatasync anddefer have different execution behaviors, and using them together can lead to inconsistent results across different browsers.

According to the HTML specification, if bothasync anddefer are present, theasync attribute takes precedence in modern browsers, while older browsers that do not supportasync will fall back todefer.

This can be useful if you want to useasync but also want to provide a fallback for older browsers. However, it's important to use this combination intentionally and understand the potential cross-browser implications.

Conclusion

In conclusion,async anddefer are powerful tools for controlling how your JavaScript files are loaded and executed.

They can help improve the performance of your website, but they should be used with care. Always consider the dependencies between your scripts and the potential cross-browser implications before deciding which attribute to use.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Web Developer who loves experimenting with SEO and Frameworks
  • Location
    Hicksville, NY
  • Pronouns
    he/him
  • Work
    Web Developer
  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp