ALI KORPEEST. 2024
Engineering

Optimizing React Performance: Beyond useMemo

Ali Korpe
Ali Korpe
April 15, 2024 2 min read
Optimizing React Performance: Beyond useMemo

The Premature Optimization Trap

We've all been there—wrapping every prop in useMemo and every handler in useCallback hoping it makes the app faster. Spoiler: it usually doesn't. In fact, it often adds overhead.

Profiling First

Before making any changes, you need to know what is slow. React DevTools Profiler is your best friend here.

  1. Record a session where you interact with the laggy component.
  2. Look for yellow bars in the Flamegraph.
  3. Check 'Why did this render?'

Virtualization key for Large Lists

If you're rendering a list of 10,000 items, no amount of memoization will save you. You need windowing.

import { FixedSizeList as List } from 'react-window';

const Row = ({ index, style }) => (
  <div style={style}>Row {index}</div>
);

const Example = () => (
  <List
    height={150}
    itemCount={1000}
    itemSize={35}
    width={300}
  >
    {Row}
  </List>
);

By rendering only what's visible, we reduced the DOM node count from 10,000 to ~20.

When to actually use useMemo

Use it when:

  1. You are engaging in expensive calculations (filtering/sorting 1000+ items).
  2. You are passing props to a child heavily optimized with React.memo.
  3. To prevent useEffect from firing too often if the dependency is an object/array.

Start with clean code, profile, and then optimize.

Tags:#React#Optimization#JavaScript#Web Vitals
Ali Korpe

About Ali Korpe

Software developer specializing in Photoshop automation and efficient workflows. Building tools that help creators save time and focus on what matters. Creator of Auto Bulk Mockup.

Get plugin tips in your inbox

Join 2,000+ developers and creators. I share tips on Photoshop automation, UXP development, and indie hacking. No spam, ever.

Have a project in mind?

Whether you need a custom plugin, design work, or just want to say hello.

Let's Work Together