Skip to content

Lesson 13 of 13 · 20m

The Script Tag Has Three Speeds

Why a plain script tag blocks the parser, when `async` versus `defer` versus `type="module"` is correct, and the two event-loop queues that decide when your callback runs.

A <script src> tag in the middle of your HTML is a blocking operation, and the reason is embarrassing and real: document.write. A script can inject markup into the stream while the parser is running, so the parser must stop, download the script, execute it, and only then continue. That 1995 design decision is why "put scripts at the bottom of <body>" was the standard advice for fifteen years — and why the attributes that fix it exist.

The three speeds

<script src="analytics.js" async></script>
<script src="app.js" defer></script>
<script src="app.mjs" type="module"></script>
  • Plain — blocks the parser at the tag. Download, execute, resume.
  • async — downloads in parallel and executes the instant it finishes, interrupting the parse, in whatever order scripts arrive. For independent scripts only: analytics, ads, a widget that touches nothing else.
  • defer — downloads in parallel, executes after the parse finishes, in document order, before DOMContentLoaded. The correct default for app code since the mid-2010s.
  • type="module" — deferred by default: all defer semantics plus strict mode, real import/export, top-level await, and import.meta.url. Shipped in every engine by mid-2018 (Chrome 61, Safari 10.1, Firefox 60, Edge 16). This is what modern projects actually ship.

And document.write is so thoroughly a hazard that Chrome since late 2016 simply refuses to execute document.write-injected scripts for users on slow connections. The platform got tired of the bug and legislated around it.

The event loop: two queues, not one

JavaScript runs on a single thread driven by a loop that alternates between two queues. Macrotasks — timers, network completions, I/O, user events — run one at a time, with rendering opportunities between them. Microtasks — promise continuations, queueMicrotask, MutationObserver — drain completely between macrotasks and before paint.

The consequence juniors discover the hard way:

Promise.resolve().then(function loop() {
  Promise.resolve().then(loop);
});

This starves the page — an infinite microtask chain never lets the loop reach rendering. The same loop with setTimeout(0) renders fine, because each iteration is a separate macrotask with paint between. await suspends only the async function it sits in — it never blocks the thread — and requestAnimationFrame aligns work to the frame budget. scheduler.yield() (Chrome 129, 2024) is the emerging way to hand control back mid-task without the setTimeout hack.

The operational details

  • DOMContentLoaded fires after the parse completes — deferred scripts run before it; load waits for every image and subresource. Analytics on load, app init on DOMContentLoaded or just defer.
  • crossorigin on a <script> is not decoration — without it, errors from CDN scripts report as the opaque "Script error." with no stack, because cross-origin errors are deliberately masked.
  • integrity="sha384-..." — Subresource Integrity — pins the expected hash so a compromised CDN can't serve you different bytes. Standard hygiene for third-party script tags.
  • type="importmap" declares bare-specifier resolution ("lodash" -> "/vendor/lodash.js") so modules can import real package names without a bundler — shipped everywhere since 2023.
  • nomodule is dead code — every current browser supports modules; the fallback era ended.

Modules changed delivery, not just syntax

<script type="module"> never blocks, dedupes imports, and enables import() for code-splitting — dynamic imports resolve to promises, so you load the charting library the first time a chart is needed instead of at startup. Combine it with AbortSignal.timeout inside the dynamic path and you've replaced a build-system feature with platform primitives.

Try it yourself

const order = [];
order.push("start");
setTimeout(() => order.push("macrotask"), 0);
Promise.resolve().then(() => order.push("microtask"));
order.push("end");
console.log(order); // start, end, microtask, macrotask

The practice

defer or type="module" for your code; async strictly for scripts that touch nothing else; never document.write. Know which queue your callback lands in before you assume when it runs.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.