React · Chapter 21 of 26

Performance

By the end of this chapter you can measure what renders and how long it takes with <Profiler>, skip a component whose props have not changed with memo, keep a function or a value the same between renders with useCallback and useMemo, explain why a new object or function prop defeats memo, and decide when all this is worth it and when it is not. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Real apps
  • 4 tasks
  • React
  • In the all labs plan

The first 2 chapters of React are free. This one comes with the all labs plan.

Why you need this

Kala Talkies has 412 seats. On Diwali night, when Parda opens the booking for Ghar Wapsi Express, a thousand phones load the seat map at once, many of them old and slow. Datta's box office laptop is one of them. Rukhsana's seat map works, but she measured it: picking one seat renders all 412 seats again, though 411 of them look exactly as they did.

Everything you have learned so far says that is fine, and most of the time it is: React renders a component, compares, and touches only what changed on the page (chapter 1). But the rendering itself, calling 412 functions and comparing 412 descriptions, takes time, and on a slow phone that time is a tap that feels late.

This chapter is about not doing work that changes nothing. First, though, the rule that every performance change follows: measure first. A change that makes code harder to read and saves nothing is a loss.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Seat.jsx: a seat that renders only when it must

    Seat gets three props: seat, an object { id, taken } like { id: 'F7', taken: false }; picked, true when this seat is picked; and onPick, a function to call with the seat's id. Show a button that says the seat's id (F7). It is disabled when seat.taken is true, has aria-pressed true when picked is true (false otherwise), and calls onPick once with the id when clicked.

    Export Seat wrapped in memo (export default memo(Seat)), so that React skips it when its props are the same as last time, and renders it again when one changes. The Check renders Seat inside a parent and counts Seat's renders: when the parent renders again with the same seat, picked and onPick, Seat must not render; when picked changes to true, it must render, with aria-pressed true. It also shows a taken seat, F4, which must be disabled.

  2. Task 2

    SeatMap.jsx: the hall, with a handler that stays the same

    SeatMap gets the hall's rows in its rows prop, each one { row: 'F', class, price, seats: [{ id, taken }, ...] }, as in hall.json. It keeps the picked seats' ids in state, in the order picked, and shows: for each row, its letter and then one Seat (imported from Seat.jsx) for each of its seats, with seat (the seat object as it is in rows), picked (true or false) and onPick; and under the hall a paragraph, Pick your seats when none is picked, or Your seats: F7, P12 with the ids in the order picked. onPick(id) picks the seat, or unpicks it when it is already picked.

    Picking one seat must render only that seat again. The Check puts its own memo Seat in place of yours and counts each seat's renders: picking F7 must render F7 and no other seat; then picking P12 must render P12 alone. So every Seat must get the same onPick function in every render, and picked as plain true or false.

  3. Task 3

    ShowtimeFinder.jsx: find showtimes only when the filters change

    ShowtimeFinder gets all of Diwali week's showtimes in its showtimes prop (as in showtimes.json: id, day, time, label, title, venue, from). It shows seven day buttons, Fri, Sat, Sun, Mon, Tue, Wed and Thu (Fri picked at first); two sort buttons, By time (picked at first) and By price; a tickets line with a button −, a paragraph 2 tickets (2 at first, from 1 to 6) and a button +; and a list, a ul (className="times"), with one li per showtime that findShowtimes(showtimes, { day, sort }) from search.js returns, in its order, each showing the time label, the title, the venue and 2 tickets from ₹240 (the tickets times the showtime's from). A picked day or sort button has aria-pressed true, the others false; sort is 'time' or 'price'.

    Call findShowtimes again only when showtimes, the day or the sort changes (keep its result with useMemo): changing the tickets must not call it. The Check renders ShowtimeFinder in StrictMode with five showtimes and counts the calls: + and − must make no call (and the li must say 3 tickets from ₹360 for Ghar Wapsi Express after +); Sat and By price must each make one with the new day or sort.

  4. Task 4 · interview question

    Interview: ShowGrid.jsx, the memo that never skips

    ShowGrid shows a search box (type search) labelled Search shows, a paragraph 24 shows (how many are shown), and one ShowCard per show whose title has the text typed in it, in any case, in shows.json's order, each with show and labels { book: 'Book', interested: 'Interested' }. ShowCard is wrapped in memo, but typing in the search box renders every card again, even those whose show has not changed.

    Fix ShowGrid.jsx (not ShowCard.jsx) so that typing renders no card whose props are the same as before; the buttons must still say Book and Interested. The Check puts its own memo ShowCard in place of the real one and counts each card's renders: typing a must leave 22 shows and render none of them again, and typing ag after it (aag) must leave 2 shows, Kaagaz Ki Nao and Raag Diwali, with no card rendered again.

    Rukhsana will ask: when is memo not worth it?

What you will learn

  • What renders and why: a component renders when its state changes or its parent renders; all of its children render with it.
  • Profiler: React's component that reports every commit, and how long the rendering took.
  • memo: skips a component when every prop is the same as last time.
  • A new object or function prop is never the same, so it defeats memo.
  • useCallback: the same function between renders, until a dependency changes.
  • useMemo: the same computed value between renders, for slow work or for memo'd children.
  • When not to bother, and the React Compiler in one paragraph.

More in Real apps

Build "Performance" yourself

Parda sells tickets for Pune's own cinemas and stages, and its booking page broke on opening night. You rebuild it in React. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of React are free. This one comes with the all labs plan.