Comparison
async scriptvsdefer script
async script
the analytics script downloaded alongside the HTML and ran the moment it arrived, whenever that was.
A script that downloads in parallel with parsing and executes as soon as it is ready, interrupting the parser at an unpredictable point. Execution order between async scripts is whatever the network decides, so it is only safe for independent scripts with no dependencies. It still blocks the main thread when it runs, so an async third-party script can still delay the first paint.
Full entry →defer script
the script downloaded in parallel but waited to run until the document had finished parsing.
A script that downloads without blocking parsing and executes after the document is parsed, in document order, before `DOMContentLoaded`. It is the right default for application code: parallel download, predictable order, no parser blocking. Module scripts are deferred automatically, which is why an inline module and a classic script can run in a surprising order.
Full entry →