Part 01 · How the Browser Works
The Life of a Page Load
Follow the ShopLite home page from the moment you press Enter to the first pixel: DNS, TCP, TLS, HTTP, parsing, layout, paint, and where the time actually goes.
⏱ 6 min · Station 1 of 125
The problem
A customer in Sylhet taps a ShopLite link on a mid-range phone. For a second and a half the screen stays white. Then the heading appears, then the product cards, and the last images arrive more than three seconds after the tap.
The server answered in a few milliseconds. So where did the time go? You can't fix a slow page until you can say which step is slow, and that means knowing every step between the tap and the pixels.
The idea
Loading a page is two relay races, one after the other. The network race gets bytes to the phone: find the server (DNS), open a connection (TCP), secure it (TLS), ask for the page (HTTP). The rendering race turns bytes into pixels: parse HTML and CSS, work out styles, compute layout, paint, and composite layers on the GPU.
Most of the steps wait on something: a round trip, a file, or the single main thread. The skill is spotting which wait dominates.
How it works
- DNS. The browser turns
shoplite.appinto an IP address. It asks a resolver, which may ask others. Cached answers are near-instant; a cold lookup can cost tens of milliseconds. - TCP. A three-way handshake: one full round trip before any data can move.
- TLS. Agree on keys and check the certificate: one more round trip with TLS 1.3 (two with TLS 1.2). On a phone with 150 ms round trips, that's already 300+ ms with zero bytes of the page delivered.
- HTTP request and response. The browser sends
GET /. The time until the first byte comes back is TTFB (server think time + one round trip). The HTML then streams in. - Parse. The parser builds the DOM from the HTML as it arrives, and the CSSOM from stylesheets. Every
<script>,<link>and<img>it finds becomes another request, which repeats steps 1-4 (minus whatever connection can be reused). - Style, layout, paint, composite. The browser matches CSS rules to elements, computes each box's size and position, records drawing commands, and hands layers to the GPU. The first time this produces visible text or images is First Contentful Paint (FCP); when the largest element appears, Largest Contentful Paint (LCP).
For a React app like ShopLite there's a second loop inside step 5: the HTML is almost empty, so nothing useful paints until the JavaScript bundle downloads, runs, calls the API, and the API's images download. Each of those is another trip through the network race.
ShopLite's home page is 71.6 KB in total. On the slow 4G profile (562 ms per request, 1.47 Mbps), roughly when does the first text appear?
Visual
Where the time goes
| Step | What it waits on | Typical cost | How you shrink it |
|---|---|---|---|
| DNS | resolver round trips | 0 (cached) to ~100 ms | dns-prefetch, fewer domains |
| TCP + TLS | 1 + 1 round trips | 2 RTT (300 ms on 150 ms RTT) | keep-alive, HTTP/2, TLS 1.3, preconnect, a CDN near users |
| TTFB | server work + 1 RTT | 5 ms to seconds | caching, CDN, faster backend |
| Download | bandwidth | size / bandwidth | compress, send less |
| Parse + run JS | main thread | ms to seconds, worse on slow CPUs | less JS, split bundles |
| Style, layout, paint | main thread | ms per frame | smaller DOM, cheaper CSS changes |
| Extra request waves | everything above, again | one RTT+ per wave | put critical data in the HTML, preload |
See it live
Measured on an Intel i3-10110U laptop (4 threads), 7 GB RAM, Ubuntu 26.04, Chrome 151 headless driven by Playwright 1.64.1, the lab from Lab Setup: ShopLite running in Docker on the same machine.
First, the network race on a real HTTPS site. curl can report when each phase finished:
# Where does the time go before the first byte? curl reports each phase of one real HTTPS request.
for i in 1 2 3; do
curl -s -o /dev/null https://example.com/ -w "run $i: dns %{time_namelookup}s tcp %{time_connect}s tls %{time_appconnect}s first byte %{time_starttransfer}s done %{time_total}s\n"
done
run 1: dns 0.063340s tcp 0.079115s tls 0.119024s first byte 0.127302s done 0.127388s
run 2: dns 0.000848s tcp 0.005634s tls 0.026199s first byte 0.038726s done 0.038791s
run 3: dns 0.000732s tcp 0.007513s tls 0.022685s first byte 0.032651s done 0.033771s
The numbers are cumulative. In run 1, DNS took 63 ms, TCP finished 16 ms later, and TLS needed another 40 ms. Runs 2 and 3 found the DNS answer in a cache (under 1 ms), so the whole request took a third as long. This server is close by (about 5 ms round trip); on a 150 ms mobile round trip, each of those handshake steps costs 150 ms instead.
Now the full page. tools/pageload.mjs opens ShopLite's home page in Chrome and reads the browser's own Navigation Timing and Resource Timing:
// node pageload.mjs [url] [profile]: load a page cold and print where the time went.
import { open, pageTimings, resources, waterfall, ms, kb, WEB } from "./lib.mjs";
const url = process.argv[2] ?? `${WEB}/`;
const profile = process.argv[3] ?? "none";
const { browser, page } = await open({ profile });
await page.goto(url, { waitUntil: "networkidle" });
const t = await pageTimings(page);
const res = await resources(page);
await browser.close();
console.log(`${url} (profile: ${profile})\n`);
console.log("phase ms");
const row = (k, v) => console.log(`${k.padEnd(28)} ${String(Math.round(v ?? 0)).padStart(6)}`);
row("DNS lookup", t.dns);
row("TCP connect", t.connect);
row("TLS handshake", t.tls);
row("request sent (from start)", t.requestStart);
row("first byte of HTML (TTFB)", t.ttfb);
row("last byte of HTML", t.responseEnd);
row("DOM ready to script", t.domInteractive);
row("DOMContentLoaded", t.dcl);
row("first contentful paint", t.fcp);
row("largest contentful paint", t.lcp);
row("load event", t.load);
console.log(`\nwaterfall (${res.length + 1} requests)`);
const rows = [{ label: "document (html)", start: 0, end: t.responseEnd }, ...res.map((r) => ({ label: r.name.split("?")[0], start: r.start, end: r.end }))];
console.log(waterfall(rows));
const bytes = t.htmlBytes + res.reduce((s, r) => s + r.bytes, 0);
console.log(`\ntotal transferred: ${kb(bytes)} FCP ${ms(t.fcp)} LCP ${ms(t.lcp)}`);
Run it once as is, once with the slow 4G profile (the same numbers Lighthouse uses for mobile: 562.5 ms per request, 1.47 Mbps down, CPU 4x slower):
http://localhost:5173/ (profile: none)
phase ms
DNS lookup 0
TCP connect 0
TLS handshake 0
request sent (from start) 5
first byte of HTML (TTFB) 8
last byte of HTML 9
DOM ready to script 25
DOMContentLoaded 71
first contentful paint 108
largest contentful paint 108
load event 71
waterfall (17 requests)
document (html) |## | 0 ms -> 9 ms
/assets/index-C1s9lTqF.js | ######## | 18 ms -> 47 ms
/assets/index-DwuM_Ger.css | #### | 19 ms -> 34 ms
/api/products | #### | 94 ms -> 110 ms
/favicon.ico | ## | 95 ms -> 100 ms
/img/10.svg | #### | 116 ms -> 132 ms
/img/128.svg | #### | 116 ms -> 132 ms
/img/239.svg | ### | 116 ms -> 126 ms
/img/291.svg | #### | 117 ms -> 133 ms
/img/370.svg | #### | 117 ms -> 133 ms
/img/399.svg | ##### | 117 ms -> 133 ms
/img/479.svg | ###### | 117 ms -> 138 ms
/img/503.svg | #########| 117 ms -> 149 ms
/img/517.svg | #########| 118 ms -> 148 ms
/img/522.svg | ########| 118 ms -> 149 ms
/img/534.svg | ########| 118 ms -> 150 ms
/img/643.svg | ########| 118 ms -> 150 ms
total transferred: 71.6 KB FCP 108 ms LCP 108 ms
http://localhost:5173/ (profile: slow4g)
phase ms
DNS lookup 0
TCP connect 0
TLS handshake 0
request sent (from start) 5
first byte of HTML (TTFB) 6
last byte of HTML 574
DOM ready to script 614
DOMContentLoaded 1563
first contentful paint 1640
largest contentful paint 1640
load event 1563
waterfall (17 requests)
document (html) |####### | 0 ms -> 574 ms
/assets/index-C1s9lTqF.js | ########### | 592 ms -> 1495 ms
/assets/index-DwuM_Ger.css | ####### | 593 ms -> 1170 ms
/api/products | ####### | 1597 ms -> 2186 ms
/favicon.ico | ####### | 1600 ms -> 2172 ms
/img/10.svg | ####### | 2201 ms -> 2768 ms
/img/128.svg | ####### | 2201 ms -> 2775 ms
/img/239.svg | ####### | 2202 ms -> 2783 ms
/img/291.svg | ####### | 2202 ms -> 2791 ms
/img/370.svg | ####### | 2202 ms -> 2799 ms
/img/399.svg | ####### | 2202 ms -> 2807 ms
/img/479.svg | ##############| 2202 ms -> 3348 ms
/img/503.svg | ############# | 2204 ms -> 3339 ms
/img/517.svg | ##############| 2204 ms -> 3356 ms
/img/522.svg | ##############| 2204 ms -> 3365 ms
/img/534.svg | ##############| 2205 ms -> 3373 ms
/img/643.svg | ##############| 2205 ms -> 3382 ms
total transferred: 71.6 KB FCP 1640 ms LCP 1640 ms
Reading it:
- DNS, TCP and TLS are 0 because the lab runs on
localhostover plain HTTP. On the internet they come first, ascurlshowed. - Four waves of requests. HTML (0-574 ms), then the JS and CSS it links (592-1495 ms), then the API call the JS makes (1597-2186 ms), then the images in the API's answer (2201-3382 ms). Each wave can only start when the previous one is done, so the latency is paid four times.
- FCP went from 108 ms to 1640 ms, and the images finished at 3.4 s. The bytes didn't change (71.6 KB both times). Only latency, bandwidth and CPU did.
- The last six images start at 2202 ms but end around 3350 ms, about 570 ms after the first six. Over HTTP/1.1 a browser opens at most 6 connections per host, so images 7-12 wait for a free one. You'll fix this in Part 5.
- Chrome's throttling adds the request latency while the response downloads, so TTFB looks tiny (6 ms) and "last byte of HTML" carries the delay. On a real network that wait sits before the first byte.
| No throttling | Slow 4G + 4x CPU | |
|---|---|---|
| HTML done | 9 ms | 574 ms |
| JS done | 47 ms | 1495 ms |
| API response | 110 ms | 2186 ms |
| FCP / LCP | 108 ms | 1640 ms |
| Last image | 150 ms | 3382 ms |
In real apps
- Put the first screen in the HTML. Large shops render the product list on the server (Part 6), so FCP happens after one wave, not three.
- Warm connections early.
<link rel="preconnect" href="https://img.cdn.example">does DNS + TCP + TLS for the image host while the HTML is still parsing. - Serve from close by. A CDN edge in Dhaka turns a 150 ms round trip to Singapore into 20 ms, and every wave gets cheaper.
- Measure in the field. Teams track TTFB, FCP and LCP from real users (Part 13), because a fast office Wi-Fi hides all of this.
What goes wrong in production
- Request chains. HTML loads JS, JS loads config, config decides which API to call, the API returns image URLs: five waves, each a full round trip on mobile.
- Too many origins. Fonts, analytics, images and the API on five different domains means five DNS + TCP + TLS setups.
- Testing only on localhost. Everything above is invisible at 0 ms latency; the same page takes 15x longer on the slow 4G profile.
- A slow TTFB hides behind a CDN for static files while the HTML itself comes from a far-away origin on every visit.
Key takeaway
A page load is a network race followed by a rendering race, and each new wave of requests runs the network race again. Count the waves and the round trips before you reach for anything else.
Practice
ShopLite's product page needs the HTML, then the JS bundle, then /api/products/42 and /api/products/42/reviews (fired together), then the product image. On the slow 4G profile, predict how many request waves there are and a rough time for the image to finish. Then run node pageload.mjs http://localhost:5173/p/42 slow4g and compare.
Answer
Four waves: HTML, then JS + CSS, then the two API calls in parallel, then the image. At about 560-600 ms per wave plus download and JS time, expect the image to finish somewhere around 2.5-3 s. In our run the API calls ended at 2193 and 2202 ms and the image at 2782 ms. The reviews response for one product is small, so it finishes with the product call. The fix is the same as for the home page: send the product data (or the whole first screen) with the HTML, and the image URL is known one wave earlier.
Station stamp
Finish the chapter
Done reading? Press Complete to save your progress. XP is Pro-only — on Pro this chapter is worth +10 XP.
Go Pro to earn XP →