13 — Performance: frontend and SQL
Commandment III applied to speed: “feels faster” doesn’t exist — before/after numbers do. Standard hygiene is assumed (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 → on hot
WHERE/JOINcolumns, 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 →, noSELECT *, 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 → +srcset, lazy loading below the fold, self-hosted fonts withfont-display: swap, server-side pagination).
Rules
- Measure first, prove after. 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 → / 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 → (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 →, 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 →, 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 →) for the front, 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 → for SQL — The query language for a database — the way you “ask” the database for data or change it. The universal standard for talking to a database. A badly written query can bog down the whole app. more in the glossary → — reported before and after. No optimization without a number; an unmeasured “optimization” can be a regression in disguise.
- 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 — otherwise there’s nothing to compare to.
- 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 →: statics are
immutablewith a longmax-age, so every asset URL carries?v=<git-short-hash>— otherwise users run the old app until a hard refresh (→ 05). - 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 token by token (SSE) — never a blank screen until generation ends (→ 15).
- Expensive aggregates are cached (a stats 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 → with a live fallback), not recounted on every request.
Mobile: the viewport is a moving target
100dvh, not100vh, for full-screen sheets — declare100vhfirst as a fallback, then100dvh(the last rule an engine understands wins).- Keyboard-sticky elements anchor to
visualViewport(height/offsetTop, re-read on itsresize/scroll), notposition: fixed; bottom: 0. Snap to the new size; don’t animate the keyboard. - Reset
flex-basiswhere the main axis flips — a row’sflex: 1 1 220pxbecomes a 220px-tall empty box in a column (flex: 0 0 autoat that breakpoint). - The emulator lies: measure real geometry (
getBoundingClientRect) and keep a device-profile test (e.g. a 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 phone): no horizontal overflow, key element above the fold.
Anti-patterns
- 🚫 Optimizing without a measurement.
- 🚫 Recomputing
COUNT(*)over the whole database on every request. - 🚫 Blocking the UI until the full LLM response arrives.
- 🚫
100vhsheets andfixed; bottom: 0inputs on mobile. - 🚫 “Fixing” mobile from a desktop-emulator screenshot.