performance Flashcards

(21 cards)

1
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the #1 rule before optimizing React performance?

A

Measure first. Use the React DevTools Profiler to identify actual bottlenecks. Premature optimization adds complexity without guaranteed benefit.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does React.memo do?

A

Wraps a component so it skips re-rendering when its props haven’t changed (shallow comparison). It’s a performance hint, not a guarantee — React may still re-render it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

When should you use React.memo?

A

When a component re-renders often with the same props, and its render is expensive. Don’t use it on every component — the shallow comparison itself has a cost.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why is React.memo useless if you pass a new object or function as a prop on every render?

A

Shallow comparison checks reference equality (===). A new object or function literal is a different reference every render, so memo always sees ‘changed’ props and re-renders anyway.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does useMemo do?

A

Caches the result of a computation between renders. It only recomputes when one of its dependencies changes. Syntax: const value = useMemo(() => expensiveCalc(a, b), [a, b]).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

When should you use useMemo?

A

When a calculation is noticeably slow (1ms+), its dependencies rarely change, or the result is passed as a prop to a memoized child and needs a stable reference.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does useCallback do?

A

Caches a function definition between renders. It returns the same function reference unless its dependencies change. Syntax: const fn = useCallback(() => { … }, [deps]).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

When should you use useCallback?

A

Primarily when passing a callback as a prop to a child wrapped in React.memo. Without useCallback, the function is recreated each render, defeating memo’s shallow comparison.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do React.memo, useMemo, and useCallback work together?

A

A typical chain: useCallback stabilizes a function reference → useMemo stabilizes a computed value → React.memo on the child skips re-render because both props are referentially equal.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What happens if you wrap everything in useMemo/useCallback unnecessarily?

A

You add overhead (dependency array comparisons, increased memory for cached values, harder-to-read code) without measurable performance gain. It can actually make things slower.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the most common source of performance problems in React apps?

A

Chains of useEffect calls that update state, causing cascading re-renders. Removing unnecessary Effects is often more impactful than adding memoization.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is code splitting and React.lazy?

A

Code splitting breaks your bundle into smaller chunks loaded on demand. React.lazy(() => import(‘./HeavyComponent’)) loads a component only when it’s first rendered, reducing initial load time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is virtualization (windowing) and when should you use it?

A

Rendering only the visible items in a long list (plus a small buffer), not all 10,000 rows. Libraries like react-window and react-virtuoso implement this. Use it for lists with 100+ items.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is debouncing in the context of React performance?

A

Delaying a state update until the user stops a rapid action (like typing). Instead of re-rendering on every keystroke, you wait for a pause — reducing total renders.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does the React DevTools Profiler show you?

A

Each commit (DOM update), which components rendered, render duration, what triggered each render (state, parent, context), and a flame graph of the component tree.

17
Q

Should you optimize in development mode or production mode?

A

Always measure in production mode. Development mode adds extra checks (like double-rendering in StrictMode) that make timings inaccurate.

18
Q

What is the React Compiler (React 19+) and how does it affect memoization?

A

It automatically analyzes components and inserts memoization where beneficial — potentially eliminating the need for manual useMemo/useCallback in most cases.

19
Q

What is a ‘pure component’ and why does it matter for performance?

A

A component that returns the same output for the same props/state. Pure components are safe to memoize and safe for React to skip re-rendering when inputs haven’t changed.

20
Q

How can moving state down (closer to where it’s used) improve performance?

A

If state is high in the tree, updating it re-renders the entire subtree. Moving it into a lower component limits re-renders to just that component and its children.

21
Q

What is composition as a performance pattern?

A

Passing children as JSX (props.children) instead of rendering them directly. The parent can re-render without re-rendering the children, because the children’s JSX was created by the grandparent.