Frontend System Design · Lesson 5 of 86 · 5 min
The Critical Rendering Path (CRP)
This is how you explain why a page is slow and what you'd do about it—a fundamental performance question.
When the browser receives HTML, it follows a sequence of steps to turn that code into pixels on the screen. This sequence is the Critical Rendering Path. If any step is slow or blocked, the user sees a blank page. Your job is to make this path as short as possible.
The path has six main steps. You should be able to name and explain each one.
- DOM Construction: The browser parses HTML into a tree of objects, the Document Object Model.
- CSSOM Construction: The browser parses CSS into a tree of styles, the CSS Object Model.
- Render Tree Construction: The browser combines the DOM and CSSOM into a Render Tree. This tree only includes what will be visible on the page. Nodes like
<head>or elements withdisplay: noneare omitted. - Layout: The browser calculates the exact size and position of every object in the Render Tree. This is also called "reflow".
- Paint: The browser draws the pixels for each element, layer by layer.
- Compositing: The browser combines the painted layers in the correct order to display the final image on screen.
This entire process is what blocks the user from seeing content.
Find the render-blocking resources
Anything the browser needs to construct the Render Tree is a "critical" resource. By default, both CSS and JavaScript are treated as critical and can block rendering.
CSS is render-blocking. The browser won't paint any pixels until it has constructed the CSSOM. It needs to know how every element will be styled before it can start drawing them. This is why a <link rel="stylesheet"> in your <head> will pause rendering until the file is downloaded and parsed.
JavaScript is parser-blocking. When the HTML parser sees a <script> tag, it stops parsing HTML. It must download, parse, and execute the script before it can continue building the DOM. The browser does this because JavaScript can modify the DOM and CSSOM (document.write is the classic example), so it has to wait and see what the script does.
The interview question: "A user reports seeing a blank white screen for three seconds after navigating to our homepage. Where do you start looking?"
Your answer starts here: "I'd first check for render-blocking resources in the
<head>of the document. A large CSS file or a synchronous JavaScript file could be delaying the initial render." Then, you'd use browser dev tools to confirm.
How to unblock the path with async and defer
You can't eliminate blocking resources, but you can control how they block. For JavaScript, you use the async and defer attributes on the <script> tag.
defer: The script is downloaded in parallel with HTML parsing, but it's only executed after the parser finishes, just before the DOMContentLoaded event. defer scripts are executed in the order they appear in the HTML. This is the best choice for scripts that need to access the full DOM.
async: The script is downloaded in parallel. As soon as it finishes downloading, HTML parsing is paused, and the script is executed. There is no guarantee async scripts will run in any particular order. Use this for independent, third-party scripts like analytics that don't depend on the DOM or other scripts.
The trap: Never use async for a script that modifies the DOM unless you are certain the elements it needs will be present. It's a race condition. If the script finishes downloading before the parser has created the target DOM node, your script will fail. Use defer for that.
For CSS, you can split your styles into critical and non-critical. Critical CSS contains the minimal styles needed to render the "above-the-fold" content. You should inline this directly in a <style> tag in the <head> for the fastest possible first paint.
Load the rest of your CSS in a non-blocking way. A common technique is to use a <link> with a media attribute that doesn't match the current context (like media="print"), then switch it to all once it loads.
Here is a single example showing these optimizations in action.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Optimized CRP</title>
<!-- 1. Inline critical CSS for the fastest first paint -->
<style>
body { font-family: sans-serif; background: #EEE; }
h1 { color: #333; }
</style>
<!-- 2. Load non-critical CSS asynchronously -->
<link rel="stylesheet" href="non-critical.css" media="print" onload="this.media='all'">
<noscript><link rel="stylesheet" href="non-critical.css"></noscript>
<!-- 3. Use 'async' for independent 3rd-party scripts -->
<script async src="analytics.js"></script>
<!-- 4. Use 'defer' for scripts that need the DOM -->
<script defer src="app.js"></script>
</head>
<body>
<h1>Welcome</h1>
<p>This content renders almost instantly because critical styles are inlined and scripts are non-blocking.</p>
</body>
</html>
What happens after the first paint
Optimizing the CRP is mostly about the initial load. But the same steps—Layout, Paint, Compositing—happen again every time you change something on the page with JavaScript. Triggering them unnecessarily causes jank and a slow user experience.
A common performance killer is layout thrashing. This happens when you repeatedly read a geometric property from the DOM (like element.offsetHeight) and then write a style that changes geometry in a loop.
Each time you read a property like offsetHeight, the browser may have to perform a layout to give you an up-to-date value. If you do this right after making a change, you force a synchronous reflow.
// BAD: Causes layout thrashing
function resizeAllParagraphs(paragraphs) {
for (let i = 0; i < paragraphs.length; i++) {
// Read (triggers layout)
const width = paragraphs[i].offsetWidth;
// Write (invalidates layout)
paragraphs[i].style.width = (width / 2) + 'px';
}
}
// GOOD: Batches reads and writes
function resizeAllParagraphsFixed(paragraphs) {
const widths = [];
// Read all values first
for (let i = 0; i < paragraphs.length; i++) {
widths.push(paragraphs[i].offsetWidth);
}
// Then write all values
for (let i = 0; i < paragraphs.length; i++) {
paragraphs[i].style.width = (widths[i] / 2) + 'px';
}
}
By batching your reads and writes, you allow the browser to perform a single layout calculation instead of one for each iteration of the loop.
Try it
Open the Chrome DevTools on a complex news or e-commerce site. Go to the "Performance" tab and record a page load. Find the "Main" thread in the flame chart and identify the long tasks. Look for the events corresponding to the CRP: "Parse HTML", "Recalculate Style", "Layout", "Paint", and "Composite Layers".
Next: DOM, CSSOM and the Render Tree
Lock it in — apply this lesson under interview conditions.