Performance: How browsers read HTML
This affects how quickly the browser can start rendering the page and show content to the user.
<!DOCTYPE html> <html lang="en"> <head> <link rel="stylesheet" href="styles.css"> <script src="large-script.js" defer></script> </head> <body> <h1>Hello World</h1> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <script src="large-script.js"></script> <link rel="stylesheet" href="styles.css"> </head> <body> <h1>Hello World</h1> </body> </html>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous script in head | Blocks DOM building until script loads | Triggers multiple reflows after script | Delays paint until script finishes | [X] Bad |
| Defer script in head | DOM builds continuously while script downloads | Single reflow after script executes | Paint happens earlier | [OK] Good |
<html>
<body>
<h1>Hello</h1>
<p>Welcome to the site.</p>
</body>
</html><html>
<head>
<title>My Page</title>
</head>
<body>
<h2>Welcome</h3>
</body>
</html>Pass the test and this pattern moves unknown