HTML Parsing and the DOM
Build an accurate mental model of what the browser does, from navigation and HTTP through parsing, rendering, storage, workers, security, and DevTools, so you can predict behavior and debug real pages.
What you will be able to do
#- Describe the parse stages from bytes to DOM nodes.
- Choose between defer, async, and module scripts.
- Identify and fix layout thrashing.
Overview
#HTML is parsed incrementally into a live tree.
The stages
Bytes are decoded to characters, tokenized into tags and text, then turned into nodes by the tree builder. The browser follows the HTML parsing algorithm, which recovers from malformed markup instead of failing.
Scripts
A parser-blocking script pauses tree construction, downloads, and executes. defer downloads in parallel and runs after parsing, in order. async downloads in parallel and runs as soon as it is ready, in any order. Module scripts are deferred by default.
The DOM is live
Reading offsetHeight or getBoundingClientRect can force the browser to flush pending style and layout work. Writing to the DOM invalidates layout. Alternating reads and writes causes layout thrashing.
// Bad: read, write, read, write
for (const el of items) {
el.style.width = el.offsetWidth + 10 + 'px';
}
// Better: batch reads, then batch writes
const widths = items.map((el) => el.offsetWidth);
items.forEach((el, i) => {
el.style.width = widths[i] + 10 + 'px';
});
Questions to carry forward
- Why does module script defer by default?
- What is the HTML tree construction algorithm?
- How do you detect layout thrashing in a trace?
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Debug
This loop is slow on a list of 5000 items. Explain the performance problem and rewrite it.
starter.javascriptfor (const el of items) { el.style.width = el.offsetWidth + 10 + 'px'; }Reveal the reference answer
reference.javascriptconst widths = items.map((el) => el.offsetWidth); items.forEach((el, i) => { el.style.width = widths[i] + 10 + 'px'; });Authored expected output: One layout flush instead of one per item.
Reading offsetWidth after writing style forces a synchronous layout each iteration. Reading all widths first, then writing, lets the browser do a single layout pass.
Exercise 2Predict
Does this script see the element with id target?
Read the code, then choose the best answer. The reference explanation stays hidden until you respond. stimulus.html<head> <script>console.log(document.getElementById('target'));</script> </head> <body> <div id="target"></div> </body>
Common misconceptions
#- The DOM is the same as the HTML source text.
- async scripts always run in document order.
- Reading offsetWidth is free.
Follow-up practice
#- browser-internals-cssom-cascade
- browser-internals-event-loop-tasks
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.