Performance Questions with Answers

8 practice questions on Performance (React & Web) with answers and step-by-step solutions. Try 8 samples below, then practise all 8 free in the AptiNest app.

Sample questions

Question 1Easy

What does React.memo do to a function component?

  1. It caches the component's HTML output in the browser for later
  2. It skips a re-render when the props are shallowly unchanged
  3. It runs the component only on the server during rendering
  4. It delays every state update until the next animation frame

Answer: B. It skips a re-render when the props are shallowly unchanged

Explanation

memo wraps a component and compares its new props with the previous ones using a shallow comparison. If they are equal, React reuses the last render instead of calling the component again.

Step-by-step solution

  1. React normally re-renders a child whenever its parent renders.
  2. memo compares the new props with the old ones, one level deep.
  3. If they match, React skips the child's render and keeps the old output.

Why the other options are wrong

  • A. memo does not cache HTML in the browser; it skips a React render.
  • C. memo works on the client and the server alike; it is not about server-only rendering.
  • D. memo does not delay state updates.

Tip: memo skips work only when props look the same; a new object or function prop breaks the match.

Question 2Easy

Why wrap a handler in useCallback before passing it to a child wrapped in memo?

  1. To make the handler run faster on each click
  2. To keep the same function identity so the memo child can skip
  3. To stop the child from ever re-rendering
  4. To cache the handler's result, so the parent reuses it

Answer: B. To keep the same function identity so the memo child can skip

Explanation

A new function is created on every render, so a memo child sees a changed prop and re-renders. useCallback returns the same function between renders while its dependencies are unchanged, so memo can skip the child.

Step-by-step solution

  1. Without useCallback, the handler is a new function each time the parent renders.
  2. The memo child sees a different prop and renders again.
  3. useCallback keeps the same function, so the child's memo check passes.

Why the other options are wrong

  • A. useCallback does not change how fast the handler runs.
  • C. The child still renders when its other props change; useCallback only keeps the handler stable.
  • D. useCallback caches the function itself, not the value it returns.

Tip: useCallback keeps a function stable; useMemo keeps a value stable.

Question 3Medium

<App n={1} /> is mounted and updated twice with the same n. How many times is "slow" logged?

function slow(n) { console.log('slow'); return n * 2; }
function App({ n }) {
  const v = slow(n);
  return <h2>{v}</h2>;
}
  1. slow, slow, slow
  2. slow
  3. slow, slow
  4. Nothing is logged

Answer: A. slow, slow, slow

Explanation

Without memoisation, slow runs on every render: mount plus two updates give three logs.

Step-by-step solution

  1. Each render calls slow.
  2. Three renders in total.

Why the other options are wrong

  • B. Without useMemo the function runs on every render.
  • C. There are three renders.
  • D. slow is called in render.

Tip: Wrap in useMemo when the computation is genuinely expensive.

Question 4Medium

A list is rendered with key={index}. A new item is inserted at the top. What goes wrong?

  1. Nothing; React matches items by key, so state follows the item
  2. Rows are matched by position, so state stays with the old slot
  3. React refuses to render this list and throws an error at runtime
  4. The whole list remounts, so no DOM or state is reused at all

Answer: B. Rows are matched by position, so state stays with the old slot

Explanation

With an index key, React treats the row at position 0 as the same item as before. The new item takes that row, so text typed into a row and other local state can appear on the wrong data.

Step-by-step solution

  1. The key is the array index, so it is 0, 1, 2 and so on.
  2. After the insert, the item at index 0 is new, but React reuses the row with key 0.
  3. Local state in that row now belongs to the wrong item.

Why the other options are wrong

  • A. Index keys do not follow items; React matches by the key value, which is the position.
  • C. Index keys are allowed; they just cause identity problems when the list changes order.
  • D. React reuses rows whose keys match, so the rows are not all remounted.

Tip: Use a stable id as the key whenever items can be inserted, removed or reordered.

Question 5Medium

A component uses the value below, and its parent re-renders it twice with the same n. How many times is expensive(n) called in total? (Production build, no StrictMode.)

function App({ n }) {
  const v = useMemo(() => expensive(n), [n]);
  return <h2>{v}</h2>;
}
  1. 2
  2. 1
  3. 3
  4. Once per render because the callback is new

Answer: B. 1

Explanation

useMemo runs its function during render only when a dependency changes. The mount and both re-renders use the same n, so expensive(n) runs once, on the first render.

Step-by-step solution

  1. The first render has no saved value, so useMemo calls expensive(n) once.
  2. The two re-renders have the same n as the dependency array.
  3. useMemo returns the saved value, so expensive is not called again.

Why the other options are wrong

  • A. The two re-renders reuse the saved value; the callback is not run again.
  • C. Three renders in total still run the callback only once.
  • D. The callback is recreated each render, but useMemo only calls it when the dependency changes.

Tip: useMemo saves a computed value; it runs again only when its dependencies change.

Question 6Hard

<App /> is mounted and the button is clicked once. What is logged?

const Ctx = createContext(null);
const Consumer = memo(function Consumer() {
  const v = useContext(Ctx);
  console.log('consumer');
  return <h2>{v.n}</h2>;
});
function App() {
  const [t, setT] = useState(0);
  const value = useMemo(() => ({ n: 1 }), []);
  return (
    <Ctx.Provider value={value}>
      <button id="b" onClick={() => setT(t + 1)}>x</button>
      <Consumer />
    </Ctx.Provider>
  );
}
  1. consumer, consumer
  2. Nothing is logged
  3. consumer, consumer, consumer
  4. consumer

Answer: D. consumer

Explanation

With a memoised value and a memo consumer, the click does not change the context or the props, so the consumer is not re-rendered.

Step-by-step solution

  1. Mount logs consumer.
  2. Click re-renders App only.
  3. Same value reference and no props: memo skips the consumer.

Why the other options are wrong

  • A. The memoised value keeps the same reference.
  • B. The consumer renders on mount.
  • C. Only one click occurred.

Question 7Hard

<App /> is mounted and the button is clicked once. What is logged?

const Ctx = createContext(null);
const Consumer = memo(function Consumer() {
  const v = useContext(Ctx);
  console.log('consumer');
  return <h2>{v.n}</h2>;
});
function App() {
  const [t, setT] = useState(0);
  return (
    <Ctx.Provider value={{ n: 1 }}>
      <button id="b" onClick={() => setT(t + 1)}>x</button>
      <Consumer />
    </Ctx.Provider>
  );
}
  1. consumer
  2. Nothing is logged
  3. consumer, consumer, consumer
  4. consumer, consumer

Answer: D. consumer, consumer

Explanation

value={{ n: 1 }} creates a new object every render, so React treats the context as changed and re-renders the consumer even though it is memoised.

Step-by-step solution

  1. Mount logs consumer.
  2. Click re-renders App with a new value object.
  3. The consumer re-renders: second log.

Why the other options are wrong

  • A. A new context value object forces the consumer to re-render.
  • B. The consumer renders on mount.
  • C. Only one click occurred.

Tip: Memoise the context value with useMemo.

Question 8Hard

The button is clicked once after the first render. How many times is 'child' logged in total? (Production build, no StrictMode.)

const Child = memo(function Child({ style }) {
  console.log('child');
  return null;
});
function App() {
  const [c, setC] = useState(0);
  return (
    <>
      <button onClick={() => setC(c + 1)}>+</button>
      <Child style={{ color: 'red' }} />
    </>
  );
}
  1. 1
  2. 2
  3. 3
  4. 0

Answer: B. 2

Explanation

The first render logs 'child'. On the click, App re-renders and creates a new style object. memo sees a different prop, so Child renders again and logs once more. The total is 2.

Step-by-step solution

  1. Mounting renders Child once, which logs 'child'.
  2. The click re-renders App, which creates a new { color: 'red' } object.
  3. A new object is not shallowly equal to the old one, so Child renders again: 2 logs.

Why the other options are wrong

  • A. Child renders on mount, so it logs at least once.
  • C. Only one click happens, and App renders once for it, so Child renders once more.
  • D. Child logs on mount even before any click.

Tip: Object and function literals in JSX are new on every render, which defeats memo.

Practise all 8 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all React & Web topics · All topics