Rzemiosło · The Craft
What is The CraftLevelsGet startedChapters Search Download PLEN
Level 3 · Codex ~2 min read

In one sentence: Measure first, then optimize: indexes, no more N+1, streaming — speed proven by numbers.

This chapter in plain terms

This chapter has one motto: performance is measurement, not a hunch. “It feels faster” doesn’t exist — there are numbers before and after. First you measure what’s slow, you fix it, and you prove with a number that it helped. Otherwise “optimisation” is sometimes a regression in disguise.

On the browser side what matters is what the user perceives as “fast”: don’t load images that aren’t visible, serve photos in a light format (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 →), and measure the whole thing with Lighthouse (built into Chrome) and the Core Web Vitals metrics.

On the database side the most common killer is a missing 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 a frequently queried column — the database then scans the whole table instead of looking under the keyword. The second is 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 →: two hundred separate queries where one would do.

A fair amount of space goes to mobile, because it’s tricky: the address bar and keyboard change the screen height on the fly. Hence 100dvh instead of 100vh and measuring the real viewport — otherwise buttons hide under the keyboard.

Example of N+1: a list of 200 products where the code asks the database for the price separately for each is 200 calls instead of one request for the whole list. The result: a “laggy” catalogue that, after adding a single bulk query, springs to life.

Read more: database index · the N+1 problem · dvh and the mobile viewport.

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/JOIN columns, 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 →, no SELECT *, 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 with font-display: swap, server-side pagination).

Rules

  1. 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.
  2. 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.
  3. 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 immutable with a long max-age, so every asset URL carries ?v=<git-short-hash> — otherwise users run the old app until a hard refresh (→ 05).
  4. 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).
  5. 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

  1. 100dvh, not 100vh, for full-screen sheets — declare 100vh first as a fallback, then 100dvh (the last rule an engine understands wins).
  2. Keyboard-sticky elements anchor to visualViewport (height/offsetTop, re-read on its resize/scroll), not position: fixed; bottom: 0. Snap to the new size; don’t animate the keyboard.
  3. Reset flex-basis where the main axis flips — a row’s flex: 1 1 220px becomes a 220px-tall empty box in a column (flex: 0 0 auto at that breakpoint).
  4. 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.
  • 🚫 100vh sheets and fixed; bottom: 0 inputs on mobile.
  • 🚫 “Fixing” mobile from a desktop-emulator screenshot.
Chapter deep-dive 13Why these rules, examples and stories from projects — what the core deliberately leaves out.

The canonical doctrine is written in English.