Engineering ยท Topic 14
๐Browser Internals & the Web Platform
14.1How a browser renders a page (the critical rendering path)
- HTML parsing โ DOM. The parser reads HTML and builds the DOM tree.
- CSS parsing โ CSSOM. Stylesheets are parsed into the CSS Object Model. CSS is render-blocking โ the browser won't paint until it has the CSSOM.
- Render tree. DOM + CSSOM combine into the render tree (only visible nodes;
display:nonenodes are excluded). - Layout (reflow). The browser computes the exact position and size of every node โ the geometry pass.
- Paint. Fills in pixels โ text, colors, borders, shadows โ into layers.
- Composite. Layers are combined (often on the GPU) into the final image on screen.
Where JavaScript fits: a plain <script> is parser-blocking โ the parser stops, downloads, and executes the script before continuing, because the script might modify the DOM. This is why scripts traditionally go at the end of <body>, or use:
deferโ download in parallel, execute after parsing completes, in order. Best default for app scripts.asyncโ download in parallel, execute as soon as ready (order not guaranteed). Good for independent third-party scripts (analytics).
Performance implications you can name:
- Layout/reflow is expensive. Changing geometry (width, height, top, margin) triggers reflow of affected nodes. Reading a layout property (
offsetHeight,getBoundingClientRect) right after a write forces a synchronous reflow โ layout thrashing. Batch reads then writes. - Composite-only properties are cheap. Animating
transformandopacitycan skip layout and paint entirely (GPU compositing) โ that's why they're the go-to for smooth animation, versus animatingleft/top/width. - Minimize render-blocking resources โ inline critical CSS, defer the rest; that's a direct LCP win.
14.2Handy platform APIs worth knowing
- IntersectionObserver โ lazy-load, infinite scroll, visibility tracking (see 3.3).
- requestIdleCallback โ run low-priority work when the main thread is idle.
- requestAnimationFrame โ schedule work right before the next paint (smooth animations, throttling to frame rate).
- Web Workers โ move heavy computation off the main thread so the UI stays responsive.
- AbortController โ cancel fetches (and other async work) โ key for cleanup (see 1.5).
๐ง Quiz: Browser Internals & the Web Platform
1 question1The critical rendering path roughly is:
Answered 0/1