Skip to content

Checkpoint: Make It Fast

The Course
•
20 min study

Close the earlier lessons and work from the brief. The starter is a session log with a chart and 10,000 sessions. Typing in the filter is slow. WindowedList from the last lesson is included.

The brief

  1. Measure first. Type one letter and read the Profiler lines. Write down what renders and how long each takes. In our tests, every keystroke cost about 30 ms for the chart, and between 60 and 230 ms for the list, depending on how many sessions matched.
  2. Stop the chart re-rendering while typing, without memo.
  3. Make the list cheap with WindowedList, keeping the same rows and the 10,000 sessions match count.

Acceptance checks:

  • Typing logs no Chart lines at all.
  • Compare the same query before and after: List update takes less time. Record your own durations; a fixed millisecond target isn't the pass condition.
  • The page holds about a dozen rows, however many match.
Your workspace React · 7 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); import { Profiler, useState } from "react"; import Chart from "./Chart.jsx"; import { logRender } from "./logRender.js"; import { sessions } from "./sessions.js"; // Typing in the filter is slow. Measure, then fix it. See the brief. export default function App() { const [query, setQuery] = useState(""); const needle = query.trim().toLowerCase(); const matches = sessions.filter((s) => s.topic.toLowerCase().includes(needle)); return ( <main> <h1>Session log</h1> <Profiler id="Chart" onRender={logRender}> <Chart /> </Profiler> <input aria-label="Filter sessions" placeholder="Filter sessions" value={query} onChange={(e) => setQuery(e.target.value)} /> <p className="muted">{matches.length.toLocaleString("en")} sessions match</p> <Profiler id="List" onRender={logRender}> <div style={{ height: 224, overflowY: "auto" }}> <ul className="cards" style={{ gap: 0 }}> {matches.map((s) => ( <li key={s.id} className="card" style={{ height: 32, boxSizing: "border-box", padding: "0.2rem 0.6rem" }}> #{s.id} {s.topic} <span className="muted">· {s.minutes} min</span> </li> ))} </ul> </div> </Profiler> </main> ); } // Pretends to draw a detailed chart: about 30 ms of work every render. export default function Chart() { const start = performance.now(); while (performance.now() - start < 30) { // Busy on purpose. } return ( <section className="panel"> <h2>This week</h2> <p className="muted">Mon 30 · Tue 45 · Wed 20 · Thu 60 · Fri 55 minutes</p> </section> ); } // Logs each render of a <Profiler> subtree: which one, why, and how long it took. export function logRender(id, phase, actualDuration) { console.log(`${id} ${phase}: ${actualDuration.toFixed(0)} ms`); } const topics = ["Reducers", "Context", "Refs", "Suspense", "Stores", "Profiling", "Testing"]; // 10,000 logged study sessions. export const sessions = Array.from({ length: 10000 }, (_, i) => ({ id: i + 1, topic: topics[i % topics.length], minutes: 10 + ((i * 7) % 50), })); import { useState } from "react"; // Renders only the rows that fit in view, plus a few either side. The rest of // the list is empty space with the right height, so the scrollbar is real. export default function WindowedList({ items, rowHeight, height, renderRow, overscan = 4 }) { const [scrollTop, setScrollTop] = useState(0); const first = Math.max(0, Math.floor(scrollTop / rowHeight) - overscan); const last = Math.min(items.length, Math.ceil((scrollTop + height) / rowHeight) + overscan); const visible = items.slice(first, last); return ( <div style={{ height, overflowY: "auto" }} onScroll={(e) => setScrollTop(e.currentTarget.scrollTop)}> <ul className="cards" style={{ height: items.length * rowHeight, position: "relative", gap: 0 }}> {visible.map((item, i) => ( <li key={item.id} style={{ position: "absolute", top: (first + i) * rowHeight, height: rowHeight, left: 0, right: 0 }}> {renderRow(item)} </li> ))} </ul> </div> ); } body { font: 15px/1.5 system-ui, sans-serif; margin: 0; background: #f4f6f9; color: #1c2430; } main { max-width: 56rem; margin: 1rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.5rem; font-size: 1.4rem; } input, select, button, textarea { font: inherit; } input, select, textarea { padding: 0.3rem 0.5rem; border: 1px solid #c4ccd6; border-radius: 6px; } button { padding: 0.25rem 0.7rem; border: 1px solid #c4ccd6; border-radius: 6px; background: white; cursor: pointer; } .toolbar { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; margin-bottom: 0.75rem; } .board { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0.75rem; } .column { background: #e9edf2; border-radius: 10px; padding: 0.6rem; min-height: 6rem; } .column h2 { margin: 0 0 0.5rem; font-size: 0.95rem; } .count { color: #5c6878; font-weight: normal; } .cards { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.4rem; } .card { background: white; border: 1px solid #d6dde6; border-radius: 8px; padding: 0.45rem 0.6rem; } .card button.title { border: 0; padding: 0; background: none; text-align: left; font-weight: 600; } .card.selected { outline: 2px solid #087ea4; } .muted { color: #5c6878; font-size: 0.85rem; } .panel { margin-top: 0.75rem; background: white; border: 1px solid #d6dde6; border-radius: 10px; padding: 0.75rem 1rem; } .panel h2 { margin: 0 0 0.25rem; font-size: 1.05rem; } .error { color: #b42318; } [role="tablist"] { display: flex; gap: 0.25rem; border-bottom: 1px solid #d6dde6; margin-bottom: 0.75rem; } [role="tab"] { border: 0; border-bottom: 2px solid transparent; border-radius: 0; background: none; padding: 0.4rem 0.8rem; } [role="tab"][aria-selected="true"] { border-bottom-color: #087ea4; color: #06617f; font-weight: 600; } .stars button { border: 0; background: none; padding: 0 0.1rem; font-size: 1.3rem; color: #b9c2cd; } .stars button[aria-checked="true"], .stars button.on { color: #d99a00; }
Runs with Vite on your computer: unzip, then npm install and npm run dev.
Hint 1: the chart

Only the filter and the list use query. Move them, with the state, into a new SessionSearch component.

Hint 2: the list

<WindowedList items={matches} rowHeight={32} height={224} renderRow={...} />, inside the List profiler.

Show one solution
One solution React · 8 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); import { Profiler } from "react"; import Chart from "./Chart.jsx"; import SessionSearch from "./SessionSearch.jsx"; import { logRender } from "./logRender.js"; export default function App() { return ( <main> <h1>Session log</h1> <Profiler id="Chart" onRender={logRender}> <Chart /> </Profiler> <SessionSearch /> </main> ); } // Pretends to draw a detailed chart: about 30 ms of work every render. export default function Chart() { const start = performance.now(); while (performance.now() - start < 30) { // Busy on purpose. } return ( <section className="panel"> <h2>This week</h2> <p className="muted">Mon 30 · Tue 45 · Wed 20 · Thu 60 · Fri 55 minutes</p> </section> ); } // Logs each render of a <Profiler> subtree: which one, why, and how long it took. export function logRender(id, phase, actualDuration) { console.log(`${id} ${phase}: ${actualDuration.toFixed(0)} ms`); } const topics = ["Reducers", "Context", "Refs", "Suspense", "Stores", "Profiling", "Testing"]; // 10,000 logged study sessions. export const sessions = Array.from({ length: 10000 }, (_, i) => ({ id: i + 1, topic: topics[i % topics.length], minutes: 10 + ((i * 7) % 50), })); import { Profiler, useState } from "react"; import WindowedList from "./WindowedList.jsx"; import { logRender } from "./logRender.js"; import { sessions } from "./sessions.js"; // The filter's state lives here, next to the only things that use it, so // typing never re-renders the chart. The list renders only visible rows. export default function SessionSearch() { const [query, setQuery] = useState(""); const needle = query.trim().toLowerCase(); const matches = sessions.filter((s) => s.topic.toLowerCase().includes(needle)); return ( <> <input aria-label="Filter sessions" placeholder="Filter sessions" value={query} onChange={(e) => setQuery(e.target.value)} /> <p className="muted">{matches.length.toLocaleString("en")} sessions match</p> <Profiler id="List" onRender={logRender}> <WindowedList items={matches} rowHeight={32} height={224} renderRow={(s) => ( <div className="card" style={{ height: 28, boxSizing: "border-box", padding: "0.2rem 0.6rem" }}> #{s.id} {s.topic} <span className="muted">· {s.minutes} min</span> </div> )} /> </Profiler> </> ); } import { useState } from "react"; // Renders only the rows that fit in view, plus a few either side. The rest of // the list is empty space with the right height, so the scrollbar is real. export default function WindowedList({ items, rowHeight, height, renderRow, overscan = 4 }) { const [scrollTop, setScrollTop] = useState(0); const first = Math.max(0, Math.floor(scrollTop / rowHeight) - overscan); const last = Math.min(items.length, Math.ceil((scrollTop + height) / rowHeight) + overscan); const visible = items.slice(first, last); return ( <div style={{ height, overflowY: "auto" }} onScroll={(e) => setScrollTop(e.currentTarget.scrollTop)}> <ul className="cards" style={{ height: items.length * rowHeight, position: "relative", gap: 0 }}> {visible.map((item, i) => ( <li key={item.id} style={{ position: "absolute", top: (first + i) * rowHeight, height: rowHeight, left: 0, right: 0 }}> {renderRow(item)} </li> ))} </ul> </div> ); } body { font: 15px/1.5 system-ui, sans-serif; margin: 0; background: #f4f6f9; color: #1c2430; } main { max-width: 56rem; margin: 1rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.5rem; font-size: 1.4rem; } input, select, button, textarea { font: inherit; } input, select, textarea { padding: 0.3rem 0.5rem; border: 1px solid #c4ccd6; border-radius: 6px; } button { padding: 0.25rem 0.7rem; border: 1px solid #c4ccd6; border-radius: 6px; background: white; cursor: pointer; } .toolbar { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; margin-bottom: 0.75rem; } .board { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0.75rem; } .column { background: #e9edf2; border-radius: 10px; padding: 0.6rem; min-height: 6rem; } .column h2 { margin: 0 0 0.5rem; font-size: 0.95rem; } .count { color: #5c6878; font-weight: normal; } .cards { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.4rem; } .card { background: white; border: 1px solid #d6dde6; border-radius: 8px; padding: 0.45rem 0.6rem; } .card button.title { border: 0; padding: 0; background: none; text-align: left; font-weight: 600; } .card.selected { outline: 2px solid #087ea4; } .muted { color: #5c6878; font-size: 0.85rem; } .panel { margin-top: 0.75rem; background: white; border: 1px solid #d6dde6; border-radius: 10px; padding: 0.75rem 1rem; } .panel h2 { margin: 0 0 0.25rem; font-size: 1.05rem; } .error { color: #b42318; } [role="tablist"] { display: flex; gap: 0.25rem; border-bottom: 1px solid #d6dde6; margin-bottom: 0.75rem; } [role="tab"] { border: 0; border-bottom: 2px solid transparent; border-radius: 0; background: none; padding: 0.4rem 0.8rem; } [role="tab"][aria-selected="true"] { border-bottom-color: #087ea4; color: #06617f; font-weight: 600; } .stars button { border: 0; background: none; padding: 0 0.1rem; font-size: 1.3rem; color: #b9c2cd; } .stars button[aria-checked="true"], .stars button.on { color: #d99a00; }
Runs with Vite on your computer: unzip, then npm install and npm run dev.

Check yourself

  1. What does actualDuration measure?
  2. Why is moving state down usually better than wrapping a component in memo?
  3. What does Activity do with a hidden component's effects?

Was this article helpful?

Your response is saved on this device.