13 — Performance: frontend and SQL
Performance isn’t an impression but a measurement. The most common optimisation mistake is guessing what’s slow and “fixing” it with no proof it was ever a problem. Speed pays back three ways at once: user comfort, search ranking (Core Web Vitals — Google’s measures: how fast a page appears, how stably it lays out and how fast it reacts. They affect your Google ranking and whether the user stays. Measurable, so you can improve them. more in the glossary →, 10) and server cost (a faster query means cheaper infrastructure, 12). Below is what stands behind the rules.
A number before and after
Measure first, prove after. ↗
For the front end the tool is Lighthouse — A free Google tool built into Chrome that measures a page’s speed, accessibility and SEO. It gives numbers instead of impressions — a reference point before and after every optimisation. more in the glossary → and Core Web Vitals: LCP — How long it takes for the main content of the page to appear (part of Core Web Vitals). It’s the first impression of speed — and a ranking signal for Google. more in the glossary → (when the main content appears), CLS — A measure of how much the page layout “jumps” while loading (part of Core Web Vitals). A jumping button means clicking the wrong thing — and a worse search ranking. more in the glossary → (how much the layout “jumps” while loading), INP — A measure of how quickly the page responds to a click or typing (part of Core Web Vitals). A page that “thinks” after a click feels broken — and loses in search. more in the glossary → (how quickly the page responds to a click). For the database — EXPLAIN QUERY PLAN — A database command that shows how it will run a query — using an index or scanning the whole table. It turns guessing “why is it slow” into a concrete answer and proof the fix worked. more in the glossary →, which tells you whether a query uses an Index (database) — A lookup in the database that makes searching instant instead of scanning everything in order. The first move for slow queries — like an index at the back of a book instead of reading 400 pages. more in the glossary → or reads the whole table row by row.
An optimisation report looks like this: “home page LCP: 3.1 s → 1.4 s after preloading the image at the top of the page and deferring rendering of the sections below the first screen”. Not “it feels lighter”. An unmeasured “optimisation” is often a regression in disguise — the code is more complicated and the page is no faster (03).
The canon takes basic hygiene for granted, so briefly, here’s what it is: indexes on columns used in filters and joins; no “N+1 — A performance bug: the app queries the database 200 times instead of once, asking separately for each item. The most common cause of a “laggy” list. Like 200 phone calls instead of one request for the whole list. more in the glossary →” problem (database queries in a loop over each row instead of one join — 200 queries where one would do); fetching only the columns you need instead of SELECT *; images in WebP — A modern image format, noticeably lighter than JPG or PNG at the same quality. Lighter images mean a faster page — especially on a phone. more in the glossary → in several sizes; lazy-loading whatever isn’t on the first screen; self-hosted fonts with font-display: swap (text shows before the font loads); server-side pagination (never the whole catalogue to the browser just to show 20 items). And one search trap: LIKE '%word' with a leading wildcard switches the index off — on a hot path, full-text search is better.
A Lighthouse Baseline — A recorded “as of today” state — a measurement or a count of violations — later results are compared against. Without a reference point you can’t see whether things got better or worse — and in a ratchet it lets numbers only fall. more in the glossary → right after the first working page ↗
Without a reference point you can’t tell whether the site is getting slower. A measurement taken on day one, while the page is still simple, lets you spot the moment a new library or section added a second to the load time — instead of discovering a year later that the site “somehow got slow” (07).
A fresh version after every deploy
Cache — A temporarily remembered result, so the same thing isn’t computed again on every request. Speeds the app up, but can be a trap: a stale cache shows old data. more in the glossary →-bust per Deploy — Releasing a new version of the app “live”, where real people use it. It’s the moment a change reaches real people — which is why it happens only on an explicit “deploy”. more in the glossary →: ↗
CSS and JS files are best served with an “immutable, keep for a long time” header — the browser doesn’t ask for them on every visit, so the page loads faster. The catch: after a new version is deployed, the browser keeps using the old file, because it was supposed to be immutable. The user sees a broken layout — new HTML with old CSS — until a hard refresh. The fix: a short git version hash is appended to every file’s address (?v=<short-hash>). Every deploy changes the address, so the browser fetches a fresh file (05).
An AI answer word by word
Streaming — Sending a large response in chunks, as it goes, instead of loading everything into memory at once. The app doesn’t choke on huge data — the user sees a result sooner. more in the glossary → LLM (language model) — An AI model trained on huge amounts of text that understands and generates language — e.g. Claude. Great with unstructured text, but more expensive and less predictable than ordinary code. more in the glossary → responses ↗
A language model takes several to a dozen-plus seconds to generate a long answer. Waiting for all of it means a blank screen or a spinner — and after three seconds the user assumes something broke. Sending the answer token by token (e.g. via SSE) makes the first words appear almost immediately. The total time is the same, but it feels completely different (15).
Expensive aggregates are cached ↗
The “1,240 products, 18,000 prices, 35 shops” line on the home page doesn’t have to be computed with COUNT(*) over the whole database on every visit. In the reference project it’s a Snapshot — A saved state of data from a specific moment — e.g. a database copy or a file of computed statistics. It speeds things up and keeps them safe, but is stale from the moment it’s taken — you need to know where fresh data comes from. more in the glossary → file refreshed by the data Pipeline — A chain of processing steps run in order, e.g. fetch → clean → enrich → check. When each step runs on its own and repeats safely, a failure halfway doesn’t mean starting from zero. more in the glossary →, falling back to the live database when the snapshot is stale (11).
The phone: a screen that moves
100dvh, not100vh, for full-screen sheets ↗
On a phone the visible area of the page isn’t a fixed rectangle: the address bar hides and reappears as you scroll, and the on-screen keyboard takes up the bottom half. The 100vh unit means the largest possible height, so a “full-screen” panel slides under the address bar and its bottom is invisible. 100dvh (dynamic height) tracks the real area. The order of declarations matters: first height: 100vh as a fallback for older browsers, then height: 100dvh — the browser takes the last rule it understands.
Keyboard-sticky elements anchor to
visualViewport↗
A chat input or a button stuck to the bottom with position: fixed; bottom: 0 is positioned relative to the page layout, not to what’s actually visible — and once the keyboard opens, it hides underneath. The only reliable anchor is the visualViewport API — An agreed way for two programs to talk to each other — one asks, the other answers in a set format. Through an API your app connects to outside services (payments, maps, AI). Treat an API key like a password. more in the glossary → (its height and offset, read on resize and scroll); it also smooths over the differences in keyboard behaviour between iOS and Android. After a resize the element jumps to its new place — animating that produces a visible jolt.
Reset
flex-basiswhere the main axis flips ↗
A subtle CSS trap: flex: 1 1 220px in a row means “roughly 220 px wide”. When the layout switches to a column on a narrow screen, the same rule means “220 px tall” — and a tall empty box appears. At that breakpoint the basis has to be reset (flex: 0 0 auto).
An emulator isn’t a phone
The emulator lies: ↗
Device mode in a desktop browser renders dvh units and sticky elements differently from a real phone. “Fixing” the layout from an emulator screenshot often fixes an emulator artefact and breaks the real phone. A surer path: measure the real geometry of elements (getBoundingClientRect(), getComputedStyle()) and have a test on a device profile — e.g. a separate Playwright — A tool that drives a real browser in tests: it clicks, types and checks what’s on screen. It checks the app the way a user will see it — including on a phone profile. more in the glossary → project pinned to a specific phone — that checks on every run: no horizontal scrolling, tabs on one line, the key element visible without scrolling. Verify, don’t declare (03).