Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 37 ยท JavaScript Performance

Loading Scripts: async, defer and Modules

Understand how a plain script tag blocks HTML parsing, and when to use defer, async, type=module, preload and dynamic import to load JavaScript faster.

A plain <script src="app.js"> is one of the most expensive lines in a web page. The browser stops reading the HTML, downloads the file, runs it, and only then continues. Everything after the script, including the content your visitor came for, has to wait.

The good news is that a single attribute usually fixes it. This lesson explains what each loading mode does, when to choose it and how to check the result.

The default: parser-blocking

When the HTML parser meets a classic script it must pause, because the script might change the document (for example with document.write). So it fetches the script, executes it, and resumes.

flowchart LR
  P1["Parse HTML"] --> P2["Download JS"] --> P3["Run JS"] --> P4["Parse rest of HTML"]

Parsing stops during both the download and the execution.

Because it blocks, a script in the <head> delays the first paint. That is why old advice said "put scripts at the bottom of the body". Modern attributes are better than that trick.

defer

defer downloads the script in parallel with HTML parsing and runs it after the document has been parsed, just before DOMContentLoaded. Deferred scripts run in the order they appear.

HTML
<script src="/js/vendor.js" defer></script>
<script src="/js/app.js" defer></script>

Use defer for most of your own code: it needs the DOM, and order matters. It can sit in the <head>, which lets the browser discover and start downloading it early.

async

async also downloads in parallel, but it runs as soon as the file arrives, pausing the parser at that moment. Async scripts run in whatever order they finish, and they may run before the DOM is complete.

HTML
<script src="https://stats.example.com/tracker.js" async></script>

Use async for independent scripts that need neither the DOM nor other scripts: analytics, ads, widgets. Do not use it for code that depends on another script.

Attribute Download Runs Order kept? Waits for DOM?
none blocks parser immediately yes no
defer parallel after parsing yes yes
async parallel on arrival no no

type="module"

Module scripts (<script type="module">) are deferred by default and may import other modules. The browser builds the dependency graph and fetches the pieces in parallel. Adding async to a module script makes it run as soon as its graph is ready.

HTML
<script type="module" src="/js/main.js"></script>
<!-- Old browsers ignore the module script above and run this one instead -->
<script nomodule src="/js/legacy-bundle.js"></script>

Modules are also strict mode by default and have their own scope, which prevents accidental globals.

See the order for yourself

The page below has an inline plain script, a module script and a DOMContentLoaded listener. Read the order of the lines in the output.

Order of execution

You should see the plain script first, then the module, then DOMContentLoaded: modules run after parsing but before that event fires.

Dynamic loading

Scripts you add from JavaScript are async by default. You can load code only when needed, for example a chart library when the user opens a report tab.

Load code on demand

The import() call returns a promise and fetches the module on demand. Bundlers use the same mechanism for code splitting, covered in the next lesson.

Hints: preload, modulepreload and fetchpriority

You can help the browser find important scripts earlier:

HTML
<link rel="preload" href="/js/critical.js" as="script">
<link rel="modulepreload" href="/js/main.js">
<script src="/js/hero-carousel.js" defer fetchpriority="low"></script>
  • preload fetches a file at high priority without running it.
  • modulepreload fetches and parses a module and its imports ahead of time.
  • fetchpriority="low" tells the browser a script is less important than other resources.

Preload only what is truly needed on this page; too many hints compete for bandwidth and make the page slower.

Third-party scripts

Tags, chat widgets and ad code are the most common cause of slow main threads. Load them with async or defer, load them late (after the load event or on user interaction), and review them regularly. Every third-party script is code you did not write running on your visitors' devices.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which attribute keeps scripts in document order and runs them after parsing?

    Deferred scripts download in parallel, run after the HTML is parsed, and keep their relative order.
  2. 2. Which script is the best fit for async?

    Async scripts run in unpredictable order, so only use them when the script is independent.
  3. 3. When does <script type="module"> execute, by default?

    Module scripts are deferred by default.
  4. 4. What does rel="preload" with as="script" do?

    Preload fetches the resource early; execution still happens where the script tag is.

Summary

  • Plain scripts block the HTML parser; avoid them in the head.
  • defer is the default choice for your own code; async suits independent third-party scripts.
  • Module scripts are deferred by default and can be imported on demand with import().
  • Resource hints help only when used sparingly.

Next: Bundle Size and Code Splitting