A slow website loses visitors: many people leave if a page takes more than a few seconds to appear, especially on a mobile connection. Speed also affects how Google ranks your pages. Web performance means making pages load quickly and respond smoothly. This lesson explains how a browser loads a page, the attributes that remove common delays, Google’s Core Web Vitals, and how to measure speed yourself.
In this lesson you will learn
- What blocks a page from appearing
- Loading scripts with
deferandasync - Resource hints:
preconnectandpreload - Core Web Vitals: LCP, INP and CLS
- Measuring with the Performance API and Lighthouse
You should know meta tags and responsive images.
How a Page Loads
The browser downloads the HTML and reads it from top to bottom. When it meets a stylesheet, it waits for it before drawing anything, so the page does not flash unstyled. When it meets a normal <script>, it stops reading the HTML until the script has downloaded and run. These render-blocking files are the main reason pages show a blank screen.
A Fast Page Head
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Fast page head</title>
<!-- Connect early to a server you will need -->
<link rel="preconnect" href="https://fonts.example.com">
<!-- Fetch the main hero image early: it is the largest thing on screen -->
<link rel="preload" as="image" href="hero.webp">
<!-- Small critical CSS inline; the full stylesheet as normal -->
<style>body { margin: 0; font-family: system-ui, sans-serif; }</style>
<link rel="stylesheet" href="site.css">
<!-- Scripts that do not block the page -->
<script src="app.js" defer></script>
<script src="analytics.js" async></script>
</head>
<body>
<img src="hero.webp" alt="Students in a classroom" width="1200" height="600" fetchpriority="high">
<!-- Images further down load only when needed -->
<img src="gallery-1.webp" alt="Science fair" width="600" height="400" loading="lazy">
</body>
</html>
deferdownloads the script in the background and runs it after the HTML has been read, in the order written. Use it for most of your own scripts.asyncalso downloads in the background but runs the script as soon as it arrives, in any order. Use it for independent scripts such as analytics.rel="preconnect"opens the connection to another server early, saving time when files are later requested from it.rel="preload"fetches an important file the browser would otherwise discover late, such as a hero image set by CSS or a font.- A small block of critical CSS inline lets the top of the page appear quickly.
fetchpriority="high"tells the browser the hero image matters most, whileloading="lazy"delays images further down.
Core Web Vitals
| Metric | Measures | Good score | Typical fixes |
|---|---|---|---|
| LCP – Largest Contentful Paint | When the main content (often the hero image) appears | 2.5 s or less | Smaller images, preload, fewer blocking files |
| INP – Interaction to Next Paint | How quickly the page reacts to clicks and taps | 200 ms or less | Less JavaScript, shorter tasks |
| CLS – Cumulative Layout Shift | How much content jumps while loading | 0.1 or less | Width and height on images, reserved space for ads |
Google measures these from real Chrome users and treats them as a ranking signal, so they matter for SEO as well as for visitors.
Measuring with JavaScript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Measure load time</title>
<style>
body { font-family: sans-serif; margin: 12px; font-size: 14px; }
.box { width: 300px; max-width: 100%; aspect-ratio: 3 / 1; background: #eceff1; border-radius: 6px; }
li { margin: 2px 0; }
</style>
</head>
<body>
<div class="box"></div>
<ul id="out"></ul>
<script>
function add(label, value) {
var li = document.createElement("li");
li.textContent = label + ": " + value;
document.getElementById("out").appendChild(li);
}
window.addEventListener("load", function () {
// performance.now() counts milliseconds since this page started loading
add("Time until load event", performance.now().toFixed(1) + " ms");
var nav = performance.getEntriesByType("navigation")[0];
if (nav) add("DOM ready after", nav.domContentLoadedEventEnd.toFixed(1) + " ms");
add("Files fetched", performance.getEntriesByType("resource").length);
});
</script>
</body>
</html>
Result
performance.now()returns the milliseconds since the page started loading, with fractions of a millisecond.performance.getEntriesByType("navigation")gives detailed timings of the page load, such as when the DOM was ready.getEntriesByType("resource")lists every file the page fetched, so you can spot large or slow ones. This tiny demo needs almost none.- Your numbers will differ each time and on each device, which is why real measurements use many visits.
Tools to Test Your Site
- Lighthouse, built into Chrome DevTools: scores performance, accessibility and SEO, with suggestions.
- PageSpeed Insights: runs Lighthouse online and shows real-user Core Web Vitals.
- DevTools Network panel: shows every file, its size and timing. Use throttling to simulate a slow mobile network.
Common Mistakes
- Uploading huge images straight from a camera or phone.
- Plain
<script>tags in the head withoutdeferorasync. - Loading many fonts and libraries for small effects.
- Testing only on a fast laptop and Wi-Fi. Many visitors use mid-range phones on mobile data.
Practice Exercises
Exercise 1. A page loads menu.js, which needs the HTML to exist, and stats.js, which works independently. Write both script tags.
Show answer
<script src="menu.js" defer></script>
<script src="stats.js" async></script>
Exercise 2. A page’s text jumps down when the banner image loads. Which Core Web Vital suffers, and what is the fix?
Show answer
CLS (Cumulative Layout Shift). Add width and height attributes to the image so the browser reserves its space in advance.
Frequently Asked Questions
What is caching?
The browser keeps copies of files it has already downloaded. The server’s Cache-Control header says for how long, so a returning visitor loads the site much faster.
What is a CDN?
A content delivery network keeps copies of your files on servers around the world, so each visitor downloads them from a nearby location, reducing delay.
Summary
- Stylesheets and normal scripts block rendering; use
deferorasyncfor scripts. preconnectandpreloadfetch important things earlier.- Core Web Vitals: LCP for loading, INP for responsiveness, CLS for visual stability.
- Measure with Lighthouse, PageSpeed Insights and the Performance API.
Next: Dark Mode with CSS.
