This site runs best with JavaScript enabled.

When to useMemo and useCallback

June 04, 2019

Video Blogger

Photo by Jp Valery


Performance optimizations ALWAYS come with a cost but do NOT always come with a benefit. Let's talk about the costs and benefits of useMemo and useCallback.

Current Available Translations:

Here's a candy dispenser:

Candy Dispenser

Available Candy
  • snickers
  • skittles
  • twix
  • milky way

Here's how it's implemented:

1function CandyDispenser() {
2 const initialCandies = ['snickers', 'skittles', 'twix', 'milky way']
3 const [candies, setCandies] = React.useState(initialCandies)
4 const dispense = candy => {
5 setCandies(allCandies => allCandies.filter(c => c !== candy))
6 }
7 return (
8 <div>
9 <h1>Candy Dispenser</h1>
10 <div>
11 <div>Available Candy</div>
12 {candies.length === 0 ? (
13 <button onClick={() => setCandies(initialCandies)}>refill</button>
14 ) : (
15 <ul>
16 {candies.map(candy => (
17 <li key={candy}>
18 <button onClick={() => dispense(candy)}>grab</button> {candy}
19 </li>
20 ))}
21 </ul>
22 )}
23 </div>
24 </div>
25 )
26}

Now I want to ask you a question and I want you to think hard about it before moving forward. I'm going to make a change to this and I want you to tell me which will have the better performance characteristics.

The only thing I'm going to change is wrap the dispense function inside React.useCallback:

1const dispense = React.useCallback(candy => {
2 setCandies(allCandies => allCandies.filter(c => c !== candy))
3}, [])

Here's the original again:

1const dispense = candy => {
2 setCandies(allCandies => allCandies.filter(c => c !== candy))
3}

So here's my question, in this specific case, which of these is better for performance? Go ahead and submit your guess (this is not recorded anywhere):

Let me give you some space to not spoil the answer for you...

Keep scrolling... You did answer, didn't you?

There, that should do it...

Why is useCallback worse?!

We hear a lot that you should use React.useCallback to improve performance and that "inline functions can be problematic for performance," so how could it ever be better to not useCallback?

Just take a step back from our specific example, and even from React and consider this: Every line of code which is executed comes with a cost. Let me refactor the useCallback example a bit (no actual changes, just moving things around) to illustrate things more clearly:

1const dispense = candy => {
2 setCandies(allCandies => allCandies.filter(c => c !== candy))
3}
4const dispenseCallback = React.useCallback(dispense, [])

And here's the original again:

1const dispense = candy => {
2 setCandies(allCandies => allCandies.filter(c => c !== candy))
3}

Notice anything about these? Let's look at the diff:

1const dispense = candy => {
2 setCandies(allCandies => allCandies.filter(c => c !== candy))
3 }
4+ const dispenseCallback = React.useCallback(dispense, [])

Yeah, they're exactly the same except the useCallback version is doing more work. Not only do we have to define the function, but we also have to define an array ([]) and call the React.useCallback which itself is setting properties/running through logical expressions etc.

So in both cases JavaScript must allocate memory for the function definition on every render and depending on how useCallback is implemented, you may get more allocation for function definitions (this is actually not the case, but the point still stands). This is what I was trying to get across with my twitter poll here:

Granted, I had several people tell me that was worded poorly, so my apologies if you got the wrong answer but actually knew the correct answer.

I'd like to mention also that on the second render of the component, the original dispense function gets garbage collected (freeing up memory space) and then a new one is created. However with useCallback the original dispense function wont get garbage collected and a new one is created, so you're worse-off from a memory perspective as well.

As a related note, if you have dependencies then it's quite possible React is hanging on to a reference to previous functions because memoization typically means that we keep copies of old values to return in the event we get the same dependencies as given previously. The especially astute of you will notice that this means React also has to hang on to a reference to the dependencies for this equality check (which incidentally is probably happening anyway thanks to your closure, but it's something worth mentioning anyway).

How is useMemo different, but similar?

useMemo is similar to useCallback except it allows you to apply memoization to any value type (not just functions). It does this by accepting a function which returns the value and then that function is only called when the value needs to be retrieved (which typically will only happen once each time an element in the dependencies array changes between renders).

So, if I didn't want to initialize that array of initialCandies every render, I could make this change:

1- const initialCandies = ['snickers', 'skittles', 'twix', 'milky way']
2+ const initialCandies = React.useMemo(
3+ () => ['snickers', 'skittles', 'twix', 'milky way'],
4+ [],
5+ )

And I would avoid that problem, but the savings would be so minimal that the cost of making the code more complex just isn't worth it. In fact, it's probably worse to use useMemo for this as well because again we're making a function call and that code is doing property assignments etc.

In this particular scenario, what would be even better is to make this change:

1+ const initialCandies = ['snickers', 'skittles', 'twix', 'milky way']
2 function CandyDispenser() {
3- const initialCandies = ['snickers', 'skittles', 'twix', 'milky way']
4 const [candies, setCandies] = React.useState(initialCandies)

But sometimes you don't have that luxury because the value is either derived from props or other variables initialized within the body of the function.

The point is that it doesn't matter either way. The benefits of optimizing that code is so minuscule that your time would be WAY better spent worrying about making your product better.

What's the point?

The point is this:

Performance optimizations are not free. They ALWAYS come with a cost but do NOT always come with a benefit to offset that cost.

Therefore, optimize responsibly.

So when should I useMemo and useCallback?

There are specific reasons both of these hooks are built-into React:

  1. Referential equality
  2. Computationally expensive calculations

Referential equality

If you're new to JavaScript/programming, it wont take long before you learn why this is the case:

1true === true // true
2false === false // true
31 === 1 // true
4'a' === 'a' // true
5
6{} === {} // false
7[] === [] // false
8() => {} === () => {} // false
9
10const z = {}
11z === z // true
12
13// NOTE: React actually uses Object.is, but it's very similar to ===

I'm not going to go too deep into this, but suffice it to say when you define an object inside your React function component, it is not going to be referentially equal to the last time that same object was defined (even if it has all the same properties with all the same values).

There are two situations where referential equality matters in React, let's go through them one at a time.

Dependencies lists

Let's review an example.

Warning, you're about to see some seriously contrived code. Please don't nit-pick that and just focus on the concepts please, thank you.

1function Foo({bar, baz}) {
2 const options = {bar, baz}
3 React.useEffect(() => {
4 buzz(options)
5 }, [options]) // we want this to re-run if bar or baz change
6 return <div>foobar</div>
7}
8
9function Blub() {
10 return <Foo bar="bar value" baz={3} />
11}

The reason this is problematic is because useEffect is going to do a referential equality check on options between every render, and thanks to the way JavaScript works, options will be new every time so when React tests whether options changed between renders it'll always evaluate to true, meaning the useEffect callback will be called after every render rather than only when bar and baz change.

There are two things we can do to fix this:

1// option 1
2function Foo({bar, baz}) {
3 React.useEffect(() => {
4 const options = {bar, baz}
5 buzz(options)
6 }, [bar, baz]) // we want this to re-run if bar or baz change
7 return <div>foobar</div>
8}

That's a great option and if this were a real thing that's how I'd fix this.

But there's one situation when this isn't a practical solution: If bar or baz are (non-primitive) objects/arrays/functions/etc:

1function Blub() {
2 const bar = () => {}
3 const baz = [1, 2, 3]
4 return <Foo bar={bar} baz={baz} />
5}

This is precisely the reason why useCallback and useMemo exist. So here's how you'd fix that (all together now):

1function Foo({bar, baz}) {
2 React.useEffect(() => {
3 const options = {bar, baz}
4 buzz(options)
5 }, [bar, baz])
6 return <div>foobar</div>
7}
8
9function Blub() {
10 const bar = React.useCallback(() => {}, [])
11 const baz = React.useMemo(() => [1, 2, 3], [])
12 return <Foo bar={bar} baz={baz} />
13}

Note that this same thing applies for the dependencies array passed to useEffect, useLayoutEffect, useCallback, and useMemo.

React.memo (and friends)

Warning, you're about to see some more contrived code. Please be advised to not nit-pick this either but focus on the concepts, thanks.

Check this out:

1function CountButton({onClick, count}) {
2 return <button onClick={onClick}>{count}</button>
3}
4
5function DualCounter() {
6 const [count1, setCount1] = React.useState(0)
7 const increment1 = () => setCount1(c => c + 1)
8
9 const [count2, setCount2] = React.useState(0)
10 const increment2 = () => setCount2(c => c + 1)
11
12 return (
13 <>
14 <CountButton count={count1} onClick={increment1} />
15 <CountButton count={count2} onClick={increment2} />
16 </>
17 )
18}

Every time you click on either of those buttons, the DualCounter's state changes and therefore re-renders which in turn will re-render both of the CountButtons. However, the only one that actually needs to re-render is the one that was clicked right? So if you click the first one, the second one gets re-rendered, but nothing changes. We call this an "unnecessary re-render."

MOST OF THE TIME YOU SHOULD NOT BOTHER OPTIMIZING UNNECESSARY RERENDERS. React is VERY fast and there are so many things I can think of for you to do with your time that would be better than optimizing things like this. In fact, the need to optimize stuff with what I'm about to show you is so rare that I've literally never needed to do it in the 3 years I worked on PayPal products and the even longer time that I've been working with React.

However, there are situations when rendering can take a substantial amount of time (think highly interactive Graphs/Charts/Animations/etc.). Thanks to the pragmatistic nature of React, there's an escape hatch:

1const CountButton = React.memo(function CountButton({onClick, count}) {
2 return <button onClick={onClick}>{count}</button>
3})

Now React will only re-render CountButton when it's props change! Woo! But we're not done yet. Remember that whole referential equality thing? In the DualCounter component, we're defining the increment1 and increment2 functions within the component functions which means every time DualCounter is re-rendered, those functions will be new and therefore React will re-render both of the CountButtons anyway.

So this is the other situation where useCallback and useMemo can be of help:

1const CountButton = React.memo(function CountButton({onClick, count}) {
2 return <button onClick={onClick}>{count}</button>
3})
4
5function DualCounter() {
6 const [count1, setCount1] = React.useState(0)
7 const increment1 = React.useCallback(() => setCount1(c => c + 1), [])
8
9 const [count2, setCount2] = React.useState(0)
10 const increment2 = React.useCallback(() => setCount2(c => c + 1), [])
11
12 return (
13 <>
14 <CountButton count={count1} onClick={increment1} />
15 <CountButton count={count2} onClick={increment2} />
16 </>
17 )
18}

Now we can avoid the so-called "unnecessary re-renders" of CountButton.

I would like to re-iterate that I strongly advise against using React.memo (or it's friends PureComponent and shouldComponentUpdate) without measuring because those optimizations come with a cost and you need to make sure you know what that cost will be as well as the associated benefit so you can determine whether it will actually be helpful (and not harmful) in your case, and as we observe above it can be tricky to get right all the time so you may not be reaping any benefits at all anyway.

Computationally expensive calculations

This is the other reason that useMemo is a built-in hook for React (note that this one does not apply to useCallback). The benefit to useMemo is that you can take a value like:

1const a = {b: props.b}

And get it lazily:

1const a = React.useMemo(() => ({b: props.b}), [props.b])

This isn't really useful for that case above, but imagine that you've got a function that synchronously calculates a value which is computationally expensive to calculate (I mean how many apps actually need to calculate prime numbers like this ever, but that's an example):

1function RenderPrimes({iterations, multiplier}) {
2 const primes = calculatePrimes(iterations, multiplier)
3 return <div>Primes! {primes}</div>
4}

That could be pretty slow given the right iterations or multiplier and there's not too much you can do about that specifically. You can't automagically make your user's hardware faster. But you can make it so you never have to calculate the same value twice in a row, which is what useMemo will do for you:

1function RenderPrimes({iterations, multiplier}) {
2 const primes = React.useMemo(() => calculatePrimes(iterations, multiplier), [
3 iterations,
4 multiplier,
5 ])
6 return <div>Primes! {primes}</div>
7}

The reason this works is because even though you're defining the function to calculate the primes on every render (which is VERY fast), React is only calling that function when the value is needed. On top of that React also stores previous values given the inputs and will return the previous value given the same previous inputs. That's memoization at work.

Conclusion

I'd just like to wrap this up by saying that every abstraction (and performance optimization) comes at a cost. Apply the AHA Programming principle and wait until the abstraction/optimization is screaming at you before applying it and you'll save yourself from incurring the costs without reaping the benefit.

Specifically the cost for useCallback and useMemo are that you make the code more complex for your co-workers, you could make a mistake in the dependencies array, and you're potentially making performance worse by invoking the built-in hooks and preventing dependencies and memoized values from being garbage collected. Those are all fine costs to incur if you get the performance benefits necessary, but it's best to measure first.

Related reading:

P.S. If you're among the few who worry about the move to hooks and that it forces us to define functions within our function components where we used to define functions as methods on our classes, I would invite you to consider the fact that we've been defining methods in the render phase of our components since day one... For example:

1class FavoriteNumbers extends React.Component {
2 render() {
3 return (
4 <ul>
5 {this.props.favoriteNumbers.map(number => (
6 // TADA! This is a function defined in the render method!
7 // Hooks did not introduce this concept.
8 // We've been doing this all along.
9 <li key={number}>{number}</li>
10 ))}
11 </ul>
12 )
13 }
14}

Discuss on TwitterEdit post on GitHub

Share article
loading relevant upcoming workshops...
Kent C. Dodds

Kent C. Dodds is a JavaScript software engineer and teacher. He's taught hundreds of thousands of people how to make the world a better place with quality software development tools and practices. He lives with his wife and four kids in Utah.