Intermediate8 min read

Async vs Defer in HTML

8 min read
903 words
32 sections6 code blocks

Introduction

When you add JavaScript files to your HTML pages, they can significantly slow down your website's loading speed. This happens because browsers typically stop everything to download and run each script file, making visitors wait longer to see your content. Fortunately, HTML provides two powerful attributes - async and defer - that solve this problem by controlling how and when your scripts load.

In this article, you'll learn how to use these attributes to make your web pages load faster and provide a better user experience. We'll explore what each attribute does, when to use them, and see practical examples that you can implement right away.

What is Async and Defer?

Async and defer are HTML attributes that you can add to your <script> tags to control the loading behavior of external JavaScript files. Both attributes tell the browser to download script files in the background while continuing to parse the HTML document, but they handle script execution differently.

Async Attribute

The async attribute makes scripts download and execute as soon as they're ready, without waiting for other scripts or the HTML parsing to complete.

Defer Attribute

The defer attribute makes scripts download in the background but wait to execute until the HTML document is fully parsed.

These attributes only work with external script files (those with a src attribute) and have no effect on inline scripts.

Key Features and Characteristics

Default Script Loading Behavior

Without async or defer, browsers follow this process:

  • Stop parsing HTML when encountering a <script> tag
  • Download the script file completely
  • Execute the script immediately
  • Resume HTML parsing after execution

This blocking behavior can make pages feel slow and unresponsive.

Async Characteristics

  • Scripts download in parallel with HTML parsing
  • Execute immediately when download completes
  • Don't wait for other scripts or HTML completion
  • Execution order is not guaranteed

Defer Characteristics

  • Scripts download in parallel with HTML parsing
  • Wait for HTML parsing to complete before executing
  • Execute in the order they appear in the document
  • Maintain dependency relationships between scripts

How Async and Defer Work

Basic Syntax

JavaScript
<!-- Async script -->
<script src="analytics.js" async></script>

<!-- Defer script -->
<script src="main.js" defer></script>

<!-- Regular blocking script -->
<script src="blocking.js"></script>

Loading Timeline Comparison

Normal Script Loading:

  1. HTML parsing → STOPS
  2. Script download → BLOCKS
  3. Script execution → BLOCKS
  4. HTML parsing → RESUMES

Async Script Loading:

  1. HTML parsing → CONTINUES
  2. Script download → PARALLEL
  3. Script execution → INTERRUPTS HTML parsing
  4. HTML parsing → RESUMES

Defer Script Loading:

  1. HTML parsing → CONTINUES
  2. Script download → PARALLEL
  3. HTML parsing → COMPLETES
  4. Script execution → BEGINS

Practical Examples

Example 1: Analytics Script with Async

JavaScript
<!DOCTYPE html>
<html>
<head>
    <title>My Website</title>
    <!-- Analytics doesn't need to wait for anything -->
    <script src="google-analytics.js" async></script>
</head>
<body>
    <h1>Welcome to My Site</h1>
    <p>This content loads immediately while analytics downloads.</p>
</body>
</html>

Example 2: Main Application Script with Defer

JavaScript
<!DOCTYPE html>
<html>
<head>
    <title>My App</title>
    <!-- Main script needs the DOM to be ready -->
    <script src="app.js" defer></script>
</head>
<body>
    <div id="main-content">
        <h1>My Application</h1>
        <button id="my-button">Click Me</button>
    </div>
</body>
</html>

Example 3: Multiple Scripts with Dependencies

JavaScript
<!DOCTYPE html>
<html>
<head>
    <title>Library Example</title>
    <!-- Library must load before main script -->
    <script src="jquery.js" defer></script>
    <script src="main.js" defer></script>
</head>
<body>
    <div id="content">
        <h1>Content loads fast</h1>
        <p>Scripts execute in order after HTML is ready.</p>
    </div>
</body>
</html>

Use Cases and Applications

When to Use Async

Perfect for:

  • Analytics and tracking scripts
  • Advertisement scripts
  • Social media widgets
  • Independent utility scripts
  • Scripts that don't interact with your page content

Example scenario: A Google Analytics script that just needs to track page views doesn't need to wait for your page content to load.

When to Use Defer

Perfect for:

  • Main application scripts
  • Scripts that manipulate DOM elements
  • Scripts with dependencies on other scripts
  • Scripts that need the full page to be parsed

Example scenario: A script that adds click handlers to buttons needs the buttons to exist in the DOM first.

When to Use Neither

Stick with normal loading for:

  • Critical scripts needed before page display
  • Scripts that modify the <head> section
  • Inline scripts (async/defer don't work with these)

Advantages and Benefits

Performance Benefits

  • Faster page loading: HTML parsing continues while scripts download
  • Better user experience: Content appears sooner
  • Reduced blocking: Scripts don't halt page rendering

Async Advantages

  • Fastest execution: Scripts run as soon as they're ready
  • Independent loading: Perfect for third-party scripts
  • Parallel processing: Multiple scripts can download simultaneously

Defer Advantages

  • Predictable execution: Scripts run in document order
  • DOM-ready guarantee: HTML is fully parsed before execution
  • Dependency-friendly: Later scripts can rely on earlier ones

Limitations and Considerations

Async Limitations

  • Unpredictable timing: Scripts execute when ready, not in order
  • Potential DOM issues: May run before DOM elements exist
  • Dependency problems: Can't guarantee one script loads before another

Defer Limitations

  • Delayed execution: Scripts wait for full HTML parsing
  • Order dependency: All deferred scripts must be compatible
  • Not suitable for urgent scripts: May delay time-sensitive operations

Browser Compatibility

  • Modern browsers: Full support for both attributes
  • Older browsers: May ignore these attributes and load normally
  • Fallback behavior: Scripts still work, just without optimization

Best Practices

Choosing the Right Attribute

Use async for:

JavaScript
<!-- Third-party analytics -->
<script src="analytics.js" async></script>

<!-- Social media widgets -->
<script src="twitter-widget.js" async></script>

<!-- Advertisement scripts -->
<script src="ads.js" async></script>

Use defer for:

JavaScript
<!-- Main application code -->
<script src="app.js" defer></script>

<!-- DOM manipulation scripts -->
<script src="ui-handlers.js" defer></script>

<!-- Dependent library scripts -->
<script src="library.js" defer></script>
<script src="plugins.js" defer></script>

Placement Recommendations

  • Place async scripts in the <head> section
  • Place defer scripts in the <head> section
  • Keep critical scripts without attributes in <head>
  • Consider moving non-critical scripts to before </body>

Testing Your Implementation

  • Use browser developer tools to check loading timelines
  • Test on slow connections to see the difference
  • Verify script execution order matches your expectations
  • Check that DOM-dependent scripts work correctly

Conclusion

The async and defer attributes are powerful tools for optimizing your HTML page loading performance. By understanding when to use each attribute, you can significantly improve your website's speed and user experience.

Remember the key principle: use async for independent scripts that can run anytime, and use defer for scripts that need the DOM to be ready or depend on other scripts. Start by identifying the scripts in your current projects and applying these attributes where appropriate.

Your visitors will appreciate faster-loading pages, and you'll have more control over how your JavaScript resources are loaded and executed. Begin experimenting with these attributes today to see the performance improvements in your own web projects.