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, beforeDOMContentLoaded. The correct default for app code since the mid-2010s.type="module"— deferred by default: alldefersemantics plus strict mode, realimport/export, top-levelawait, andimport.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
DOMContentLoadedfires after the parse completes — deferred scripts run before it;loadwaits for every image and subresource. Analytics onload, app init onDOMContentLoadedor justdefer.crossoriginon 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.nomoduleis 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, macrotaskThe 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?
Keep reading
Related articles from the SudoSecurity blog.
Building a Modern App With React, Vite, and Tailwind CSS: The Complete 2025 Guide
React for the UI, Vite for the build tooling, Tailwind for the styles. This is the stack most new frontend projects start with in 2025. This guide wires all three together from scratch and builds a real application structure you can actually start a production project from.
What is Tailwind CSS? The Utility-First CSS Framework Developers Either Love or Hate
Tailwind is a utility-first CSS framework. You style elements by stacking single-purpose class names directly in your markup. Half the dev community hated this idea, then started using it. Here's why it works and what you need to know.
What is Vite? The Build Tool That Made Frontend Development Fast Again
Vite replaced Webpack for a lot of teams without much fanfare. Native ES modules, a dev server that starts in milliseconds, and a production build that doesn't require understanding 300 lines of config. Here's how it works and why the switch is worth it.
What is React? The JavaScript Library That Changed Frontend Development
React is a JavaScript library for building user interfaces. Facebook built it in 2013, open-sourced it, and the entire frontend ecosystem reorganized itself around it. Here is how the component model works, why it took over, and what you need to know before writing a single line.