Web performance
An app can work and still feel slow. This note covers why typing freezes on big lists, the three Core Web Vitals (LCP, INP, CLS) and how to measure them, and the main fixes: useDeferredValue + memo, list virtualization, image size/priority and dynamic imports.
56.1 Problem: typing freezes
- SearchOrders filters 10,000 orders on every render and renders every match as a paragraph.
- Typing 99 logs Rendered on every key and the input freezes for a moment with each key.
import { useState } from "react";import { orders } from "./orders"; export function SearchOrders() { const [query, setQuery] = useState(""); const list = orders.filter((o) => o.name.includes(query)); console.log("Rendered", list.length); return ( <> <input onChange={(e) => setQuery(e.target.value)} /> {list.map((o) => <p key={o.id}>{o.name}</p>)} </> );}Search: 99 (lag on every key)Order 99Order 199Console: Rendered 10000, 3439, 28056.2 Why does typing freeze?
- Every key press changes state, React re-renders the whole component and thousands of rows are rebuilt in JSX and the DOM.
- All this work runs on the browser's main thread; while it is busy, the new letter does not even appear in the input.
- The user feels the app hung; the metric that measures this delay is INP.
- Step 1
1.Key press: query state changes
- Step 2
2.Whole component re-renders
- Step 3
3.Thousands of rows rebuilt in JSX and DOM
- Step 4
4.Main thread busy
- Step 5
5.New letter shows late: poor INP
56.3 Core Web Vitals
- Google measures page speed with three numbers; they also affect SEO ranking.
- INP replaced FID as a Core Web Vital in 2024.
| Metric | Measures | Good value | Typical fix |
|---|---|---|---|
| LCP (Largest Contentful Paint) | Time until the biggest content (hero image) shows | Up to 2.5 s | fetchPriority="high" on the hero, smaller images |
| INP (Interaction to Next Paint) | Click/key press until the next screen update | Up to 200 ms | Less main-thread work: deferred render, virtualization |
| CLS (Cumulative Layout Shift) | How much content moves while loading | Up to 0.1 | width/height on images, reserve space |
56.4 Measuring real users
- Lab tools: Lighthouse and the Performance tab in Chrome DevTools (plus the React Profiler).
- Real user (field) data: the web-vitals library.
- send prints name, value and rating; navigator.sendBeacon sends the data to the analytics server.
- INP 620 poor shows real users feel the search bug.
import { onLCP, onINP, onCLS } from "web-vitals"; function send(metric) { console.log(metric.name, metric.value, metric.rating); navigator.sendBeacon("/analytics", JSON.stringify(metric));} onLCP(send);onINP(send);onCLS(send);> LCP 2140 good> INP 620 poor> CLS 0.18 needs-improvement56.5 Fix 1: urgent update vs heavy render (useDeferredValue)
- useDeferredValue(query) creates a deferred copy of query; OrderList receives this deferred value.
- The typing update is urgent and shows immediately; the heavy list render happens in the background.
- OrderList is wrapped in memo, so it does not re-render while the deferred value stays the same.
import { useState, useDeferredValue } from "react";import { OrderList } from "./OrderList"; export function SearchOrders() { const [query, setQuery] = useState(""); const deferred = useDeferredValue(query); return ( <> <input onChange={(e) => setQuery(e.target.value)} /> <OrderList query={deferred} /> </> );}Search: 99 (smooth typing)Order 99Order 19956.6 Fix 2: list virtualization
- The screen shows about ten rows at a time, so the DOM should contain only those rows.
- react-window List gets rowComponent, rowCount and rowHeight (40); Row gets its position via style.
- Result: the DOM holds about 13 rows instead of 10,000.
import { memo } from "react";import { List } from "react-window";import { orders } from "./orders"; function Row({ index, style, items }) { return <p style={style}>{items[index].name}</p>;} export const OrderList = memo(({ query }) => { const items = orders.filter((o) => o.name.includes(query)); return <List rowComponent={Row} rowProps={{ items }} rowCount={items.length} rowHeight={40} style={{ height: 400 }} />;});Order 1Order 2Order 3Order 4DOM: only 13 rows, not 10000- Step 1
1.List is a fixed-height box that tracks the scroll position
- Step 2
2.Scroll position + row height decide which rows are visible
- Step 3
3.Render only those rows plus a few overscan rows (about 3)
- Step 4
4.On scroll, old rows are removed and new rows take their place
10,000 items in data, about 13 rows in the DOM
56.7 Images: CLS and LCP
- Checkpoint: the banner image loads late and pushes the Shop now button down just as the user clicks. Which vital got worse? CLS.
- The browser did not know the image size, so it reserved no space.
- Fix: width and height reserve the space in advance; fetchPriority="high" downloads the LCP image (the banner) first.
- loading="lazy" on images further down downloads them only when the user scrolls close.
export function Home() { return ( <main> <img src="/sale-banner.webp" alt="Diwali sale" /> <h1>Today's deals</h1> <button>Shop now</button> </main> );}# Today's deals[button] Shop now Diwali sale banner (image)# Today's deals[button] Shop nowexport function Home() { return ( <main> <img src="/sale-banner.webp" alt="Diwali sale" width={1200} height={400} fetchPriority="high" /> <h1>Today's deals</h1> <img src="/bank-offers.webp" alt="Bank offers" width={1200} height={300} loading="lazy" /> </main> );}Diwali sale banner (space reserved)# Today's deals56.8 Common mistakes
- Importing a heavy library (jsPDF) at the top: it lands in the main bundle and every user downloads it, even those who never make an invoice.
- Fix: dynamic import inside the click handler, so it becomes a separate chunk downloaded only on click.
- Believing Lighthouse 100 = fast: it is a lab test on a fast laptop, and it cannot measure INP directly (no real user clicks). Use field data from web-vitals or PageSpeed Insights.
- Images without width/height (CLS) and lazy-loading the hero image (hurts LCP).
// Wrong: jsPDF in every user's main bundleimport { jsPDF } from "jspdf"; export function InvoiceButton() { function download() { const doc = new jsPDF(); doc.text("Invoice 101", 10, 10); doc.save("invoice.pdf"); } return <button onClick={download}>PDF</button>;}// Right: separate chunk, downloaded on clickexport function InvoiceButton() { async function download() { const { jsPDF } = await import("jspdf"); const doc = new jsPDF(); doc.text("Invoice 101", 10, 10); doc.save("invoice.pdf"); } return <button onClick={download}>PDF</button>;}- Step 1
1.Measure: field data (web-vitals, PageSpeed) + Lighthouse + Profiler
- Step 2
2.Find the worst metric: LCP, INP or CLS
- Step 3
3.Fix: images, re-renders, virtualization, bundle size
- Step 4
4.Measure again and compare
Never guess: measure first
56.9 Performance rules
- Measure first, never guess: Lighthouse, the Profiler and web-vitals data.
- Images: width/height, fetchPriority="high" on the hero, loading="lazy" below the fold.
- Virtualize long lists; use useDeferredValue where typing triggers heavy work.
- Find heavy libraries with a bundle analyzer; use a dynamic import or a smaller alternative (dayjs instead of moment).
56.10 Interview tips
- Core Web Vitals: LCP = loading speed, INP = interaction response, CLS = visual stability (good: 2.5 s, 200 ms, 0.1).
- To improve: measure with field data and the Profiler, then optimize images, stop unnecessary re-renders and virtualize long lists.
- INP replaced FID in 2024.
- useDeferredValue separates the urgent input update from the heavy render.
- Lighthouse is lab data; real users decide your field metrics.