โšกLearnIT
Engineering ยท Topic 14

๐ŸŒBrowser Internals & the Web Platform

14.1How a browser renders a page (the critical rendering path)

  1. HTML parsing โ†’ DOM. The parser reads HTML and builds the DOM tree.
  2. 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.
  3. Render tree. DOM + CSSOM combine into the render tree (only visible nodes; display:none nodes are excluded).
  4. Layout (reflow). The browser computes the exact position and size of every node โ€” the geometry pass.
  5. Paint. Fills in pixels โ€” text, colors, borders, shadows โ€” into layers.
  6. 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 transform and opacity can skip layout and paint entirely (GPU compositing) โ€” that's why they're the go-to for smooth animation, versus animating left/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 question

1The critical rendering path roughly is:

Answered 0/1