Under the Hood of React Memoization-- Performance, Fiber & Optimization

Invalid Date (NaNy ago)


🚀 Understanding React Memoization Under the Hood

React is powerful because of its declarative nature, but when apps scale, re-rendering every component unnecessarily can harm performance. That’s where memoization becomes a game-changer.

This article breaks down how React's memoization techniques (like React.memo, useMemo, and Fiber Node caching) work under the hood to minimize re-renders, boost performance, and smartly manage updates.


🧠 What Is Memoization?

Memoization is an optimization technique where function outputs are cached based on their inputs. If the inputs haven’t changed, React can skip re-computing or re-rendering.


🧩 React.memo: Component-Level Memoization

✅ What It Does:

React.memo wraps a component and prevents it from re-rendering if its props haven't changed.

const MyComponent = React.memo(function MyComponent({ value }) {
  return <div>{value}</div>;
});

🧬 Under the Hood:

🔍 With Custom Comparison:

React.memo(Component, (prevProps, nextProps) => {
  return prevProps.id === nextProps.id;
});

This gives fine-grained control over memoization logic.


🧠 useMemo: Value-Level Memoization

✅ What It Does:

useMemo stores and returns a cached value unless its dependencies change.

const expensiveValue = useMemo(() => computeExpensiveThing(data), [data]);

🔬 Internals:

// Pseudocode
if (depsAreSame(oldDeps, newDeps)) {
  return cachedValue;
} else {
  const result = compute();
  cache(result);
  return result;
}

⚠️ Pitfall:

Using useMemo too aggressively can backfire. It's best for expensive computations, not general optimization.


🧱 Fiber Node Caching & Reconciliation

📚 The Fiber Architecture:

React maintains a tree of Fiber Nodes, each representing a component instance. During updates:

  1. React performs a diff of the virtual DOM.
  2. For each component, it decides whether to reuse or recreate the Fiber Node.
  3. Memoized components let React reuse subtrees entirely.
RootFiber
├── AppFiber (unchanged)
│   ├── MemoizedHeader (skipped render)
│   ├── ChangingBody (re-rendered)

🧵 Example: Fiber Bailout with Memo

const Header = React.memo(() => <h1>Static Title</h1>);

When parent re-renders but props to Header don’t change, React:

That means reduced Fiber work units, and better performance.


🛠️ Advanced Example: Memo + useMemo

const UserCard = React.memo(({ user }) => {
  const fullName = useMemo(
    () => `${user.firstName} ${user.lastName}`,
    [user.firstName, user.lastName]
  );
 
  return <div>{fullName}</div>;
});

Here:

This double-layer memoization ensures React avoids unnecessary render + compute work.


🧪 When Should You Use Memoization?

✅ Use When:

🚫 Avoid When:


🔄 Real World Profiling Tips

Use React DevTools Profiler to:


🧠 Conclusion

Memoization in React is not magic — it’s carefully engineered control over re-rendering and computation. When used wisely with React.memo, useMemo, and understanding of Fiber internals, you can significantly improve app responsiveness and resource usage.

React’s design allows you to selectively bail out of updates, saving performance where it matters most.