Show contents
Chapter 5 of 7 · 20 min
Light pages for slow networks
By the end of this chapter, you'll be able to
- Compute a page's weight and its loading time depending on the network
- Set and keep to a weight budget
- Make images lighter: dimensions, format and lazy loading
- Plan what the user sees when the network is slow or cut
Measuring a page's weight
A web page is made of several files: the HTML, style sheets, JavaScript scripts, images, fonts. Its weight is the sum of their sizes. Download time depends on the weight and the network speed (in bits per second: a byte is 8 bits).
const files = {
"index.html": 18,
"style.css": 24,
"app.js": 160,
"photo-1.jpg": 4100,
"photo-2.jpg": 3800,
"font.woff2": 45,
};
const totalKB = Object.values(files).reduce((sum, kb) => sum + kb, 0);
console.log("Total weight:", totalKB, "kB");
const networks = { "slow 3G": 0.4, "3G": 2, "4G": 10 };
for (const [network, mbps] of Object.entries(networks)) {
const seconds = (totalKB * 8) / (mbps * 1000);
console.log(`${network}: ${seconds.toFixed(1)} s`);
}
Total weight: 8147 kB
slow 3G: 162.9 s
3G: 32.6 s
4G: 6.5 s
The two photos make up 97% of the weight. These figures only count the download: in reality, you must add the time to set up connections, often longer on a mobile network.
To measure a real page, open the browser's developer tools, Network tab: it lists each file, its weight, and lets you simulate a slow connection ("Slow 3G").
A weight budget
A weight budget is a limit set in advance, for example "under 500 kB for the home page". Every new image or library must find room in the budget, or replace something else. It is the simplest way to keep a site fast over the months.
With lazy loading, an image at the bottom of the page is only downloaded if the person scrolls down to it.
Text description of the animation
- The photo comes straight from the camera: 12 million pixels, while the phone screen shows about 360 across.
- It is resized to the size it will be displayed at (with some margin for high-density screens).
- A modern format such as WebP or AVIF reduces the weight further, with no visible difference.
- With lazy loading, an image at the bottom of the page is only downloaded if the person scrolls down to it.
Making images lighter
Images are almost always the biggest part of the weight. Three moves cover most of it:
<img
src="fridge-800.webp"
srcset="fridge-400.webp 400w, fridge-800.webp 800w"
sizes="(min-width: 48rem) 50vw, 100vw"
width="800" height="600"
loading="lazy"
alt="Two-door fridge, silver colour">
- resize: prepare the image at its display size, and offer several sizes with
srcset, so the browser picks the smallest one that is enough; - compress in a modern format (WebP, AVIF);
- defer with
loading="lazy"the images that are not visible on load.
And do not forget the alt text: it describes the image to people using a screen reader, and is shown when the image fails to load.
Planning for slow and dropped networks
Even a light page can load slowly. A few good practices:
- show the text first: it weighs little and can be read while the images arrive;
- show a loading state when waiting for an answer, and a clear message on failure ("No connection. Try again"), rather than a blank screen;
- keep files already downloaded for the next visits: that is the role of the cache, and a PWA goes further by working offline.
Mini-lab: your 500 kB budgetJavaScript
Output
Click “Run” to execute your code.Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor
Your turn
Worked exercise
Worked exercise: the budget checker
Write check(files, budgetKB) that prints the total weight, says whether the budget is kept, then lists the files over 100 kB, from heaviest to lightest, with their share of the total as a rounded percentage.
const files = { "index.html": 18, "app.js": 160, "hero.jpg": 420, "logo.png": 35, "map.png": 210 };
check(files, 500);
Hint 1
Object.entries(files) gives [name, kb] pairs; .filter(([, kb]) => kb > 100) keeps the heavy files.Hint 2
.sort((a, b) => b[1] - a[1]), and compute the share with Math.round((kb / total) * 100).Show the solution and common mistakes
function check(files, budgetKB) {
const total = Object.values(files).reduce((s, kb) => s + kb, 0);
console.log(`Total: ${total} kB — ${total <= budgetKB ? "budget kept" : "budget exceeded by " + (total - budgetKB) + " kB"}`);
Object.entries(files)
.filter(([, kb]) => kb > 100)
.sort((a, b) => b[1] - a[1])
.forEach(([name, kb]) => console.log(` ${name}: ${kb} kB (${Math.round((kb / total) * 100)}%)`));
}
const files = { "index.html": 18, "app.js": 160, "hero.jpg": 420, "logo.png": 35, "map.png": 210 };
check(files, 500);
Total: 843 kB — budget exceeded by 343 kB
hero.jpg: 420 kB (50%)
map.png: 210 kB (25%)
app.js: 160 kB (19%)
The big home image alone is half the weight: that is the one to make lighter first, before touching the code.
Common mistakes:
- Sorting with
.sort()and no comparison function: JavaScript then sorts as text, and "35" comes after "210". - Optimising the small files first: saving 5 kB on the HTML changes nothing when an image weighs 420 kB. Always start with the heaviest.
Summary
- 1A page's weight is the sum of its files; loading time depends on weight and speed (8 bits per byte).
- 2A weight budget, set in advance, keeps a site fast over time.
- 3Resizing, compressing (WebP, AVIF) and deferring (loading="lazy") cover most of the images' weight.
- 4Shrinking an image in CSS does not shrink its download: it is the file that must be made lighter.
- 5Show the text first, a loading state and a clear message when the network is missing.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.