concept Frontend & browsers
Streaming HTML parsing and blocking scripts
A script can finish downloading at the same time in two timelines yet leave the parser at different points in the HTML. Compare blocking, defer, and async to see when the DOM can keep growing.
What it means
A browser can build the document tree (the DOM) as it reads incoming HTML. When the parser reaches an external, parser-blocking classic script, it stops before processing the HTML that follows. It waits until the script is ready, runs it, and then resumes parsing. With defer, the parser can keep building the DOM while the same script downloads; execution waits until after parsing.
Try it
Simulation. Start with classic-blocking, then change only Script mode to defer. Compare the parser's progress at 60 milliseconds (ms), before the download has finished in either case. How much of the HTML after the script has it processed?
Streaming HTML parsing and blocking scripts
Show how parser-blocking scripts interrupt progressive DOM construction
Parser waits for script fetch and blocking stylesheet, then executes before parsing the remaining HTML.
Script scheduling
- Modeclassic-blocking
- Blocking stylesheet ready (ms)0
- Discovered (ms)10
All times are synthetic. Parser completion is an event marker, not a claim that the parser ran continuously through blocking execution.
Evaluated inputs
- Required scenario evidence
- provided
- HTML bytes before script
- 100 bytes
- Parser throughput
- 10 bytes/ms
- HTML bytes after script
- 500 bytes
- Script download after discovery
- 70 ms
- Script mode
- classic-blocking
- Blocking stylesheet ready at
- 0 ms
- Script execution duration
- 20 ms
One external classic script, fixed byte-processing rate, synthetic ready times, no other tasks, scanner, network or document.write. Blocking stylesheet applies to blocking/deferred cases under stated rules; async modeled without CSS gate. DCL at same timestamp as eligible task by convention, not browser event timing guarantee.
About this model
The controls set only a local, bounded scenario. The computed branch is not a verdict about a live system. Inputs listed as not evaluated did not participate in this branch after validation.
Reset restores the original inputs. It does not change the open or closed state of help or FAQ answers.
There are 100 bytes before the script and 500 after it. At 10 bytes/ms, the parser discovers the script at 10 ms. The download takes another 70 ms, so it finishes at 80 ms. The stylesheet is ready from the start, and execution takes 20 ms. Here's what changes when only the script mode changes:
| Milestone | classic-blocking | defer |
|---|---|---|
| HTML processed at 60 ms | 100 bytes; still at the script | All 600 bytes |
| Script download finishes | 80 ms | 80 ms |
| Script executes | 80–100 ms | 80–100 ms |
| Parser finishes | 150 ms | 60 ms |
DOMContentLoaded (DCL) marker | 150 ms | 100 ms |
In the blocking case, the parser resumes at 100 ms with all 500 trailing bytes still to process: another 50 ms of work. With defer, it does that work during the download instead. Parsing finishes at 60 ms, but DCL waits for the deferred script to finish. The simulation marks DCL as soon as it is eligible; the timestamps are not browser timing guarantees. Use Reset to return to the blocking example.
Downloaded does not always mean ready to run
From the default blocking example, set Blocking stylesheet ready at to 120 ms. That's a time measured from the start, not an extra 120 ms added to the download. The script still finishes downloading at 80 ms. For the next 40 ms it is available but cannot run because the stylesheet that blocks this script is not ready. Execution takes place from 120 to 140 ms; only then can the parser resume. Parsing and the DCL marker move to 190 ms without any change in download time.
async makes a different trade. In the simulation it does not hold the parser at discovery or wait for parsing to finish. With the original 70 ms download, parsing finishes and DCL is marked at 60 ms, before the script runs at 80–100 ms. Shorten the download to 20 ms and the script is ready at 30 ms, while parsing is still underway. Execution now interrupts parsing from 30 to 50 ms. The parser finishes at 80 ms instead of 60 ms. The earlier download moved execution into the parser's path; it did not make execution take longer. The async calculation does not use the stylesheet-ready time.
Set async discovery to 10 ms and download to 50 ms, so the fetch ends exactly at the modeled parser completion of 60 ms. Under the fixture scheduling convention, execution ends later at 80 ms while DOMContentLoaded remains at 60 ms. This equality is a boundary of the toy scheduler, not a general claim about task ordering in a browser.
In practice
When reading a browser timeline, first identify which script the parser encountered and whether it was blocking, deferred, or async. Compare its download completion with its execution interval and the parser's progress. If a blocking script was downloaded well before it ran, look for an applicable script-blocking stylesheet rather than attributing the whole pause to the script download.
Another request starting during the pause does not prove that the parser has reached that part of the document. A browser may look ahead and fetch resources speculatively while its normal parser is blocked. Exactly what it fetches this way is implementation dependent. The simulation has one external classic script, a fixed parsing rate, and supplied durations; it has no speculative scanner, other tasks, or live network measurements.
The useful question is not just “When did the file arrive?” It is “What had to finish before the parser could read the next bytes?” A download timestamp alone cannot tell you whether those bytes were already part of the DOM, still waiting on script readiness, or waiting for execution to finish.
Frequently asked questions
Editorial questions based on the listed sources and explicitly labeled local examples.
Does defer mean never executes?
No; deferred execution occurs after parsing under applicable rules.
Does async preserve document order?
Not generally. An async script can execute when it becomes ready rather than following document order. Dependencies requiring order need an appropriate execution contract.
Can preloading avoid execution time?
No. In the illustrated timeline the execution-duration input remains even when fetch ends early.
Can CSS delay script execution?
Yes, for applicable parser-inserted classic scripts. A script-blocking style sheet can delay execution after the fetch has finished. The example separates download completion and readiness to execute; it does not simulate all CSS scheduling.
Sources & model boundaries
Primary references used for this explanation. The interactive example is an educational model, not a live check.
- WHATWG HTML: script element
Section 4.12.1.1 (script processing). Classic async and defer have different execution conditions. A script-blocking style sheet can delay an applicable parser-inserted script after its download.
Checked .
- WHATWG HTML: speculative parsing
Speculative HTML parsing. A browser may discover and fetch resources speculatively while its normal parser is blocked. Exact speculative fetching is implementation dependent.
Checked .