EasyNetwork Optimization📖 Theory Question

What is the difference between defer and async on script tags?

💡

Hint

async executes as soon as downloaded (may block parsing); defer executes after HTML is parsed, in order

Full Answer

Both attributes tell the browser to download the script without blocking HTML parsing, but they differ in when the script executes.

Default (no attribute) — parsing blocks while the script is downloaded and executed. Render-blocking.

async — downloads in parallel; executes immediately when downloaded, even if HTML isn't done parsing yet. Order not guaranteed.

<script async src="analytics.js"></script>
{/* Good for: independent scripts like analytics — don't care about DOM or other scripts */}

defer — downloads in parallel; executes after HTML parsing is complete, in document order.

<script defer src="app.js"></script>
{/* Good for: scripts that need the DOM or must run in order (most app scripts) */}
Blocks parsingExecution timeOrder preserved
defaultYesImmediatelyYes
asyncNoWhen downloadedNo
deferNoAfter parseYes
💡 Modern bundlers (Vite, Next.js) automatically add defer to script tags. You mainly need to know this for third-party scripts added manually.

More Network Optimization Questions

EasyWhat is the difference between preload, prefetch, and preconnect?EasyHow does HTTP/2 multiplexing improve performance over HTTP/1.1?EasyWhat is image optimization and what techniques does Next.js Image component apply?MediumWhat is the critical rendering path and how do you optimize it?

Practice this in a timed sprint →

5 free questions, no signup required

⚡ Start Sprint