Skip to content
engineering workbench.by StudyPort Academy
Learning path
Advanced18 minLESSON 06 / 8

Performance

01

Understand the idea

React is often fast enough without extra caching. useMemo and useCallback help in specific cases — and hurt when you add them by habit.

What these two hooks do

useMemo(function, dependencies) runs the function when a dependency changes, and otherwise returns the last result.

useCallback(function, dependencies) returns the same function instance until a dependency changes.

Both are : remember work so you can skip repeating it.

They do not make slow code magically fast by themselves. They help when:

  • a calculation is genuinely expensive, or
  • a child is wrapped in React.memo and would re-render because you pass a new function every time.

When it is worth it

Cache or leave it alone
SituationReach forLeave it alone
Filtering a huge list on every keystrokeuseMemoA list of three items
Passing a function into a memoized childuseCallbackA button handler in the same component
Building an object an effect depends onOften depend on an id insteadCaching the object “just in case”

If you have not seen a slowdown, and no child is memoized, write the plain version.

Sometimes the real fix is the dependency

People add useMemo because an effect re-runs too often. Often the better fix is: depend on user.id, not on a whole user object created inline during render. New object every render means “changed” every render.

How to read the live example

  1. count starts at 1.
  2. total is count * 2 inside useMemo, so it recalculates when count changes.
  3. bump is a useCallback that increases count.
  4. The console names the lesson rule: memo, callback, skip, measure, not-every-function.

Click the button. total should update. That shows the tools working - not that every file needs them.

Default to the simple code

In review, ask what got faster. If the answer is only “I wrapped it,” ask for a measurement or a memoized child that needed a stable function. A is not automatically a problem. A wrong dependency is.

02

Mental model

FOLLOW THE VALUE

inputs
cache
same result

Dependencies are the question.

01 / 03Trace the transformation

03

Live example

The example shows a memoized total and a stable bump , then logs the rule: measure first, and do not wrap every .

import { useMemo, useCallback, useState } from "react";
export default function App() {
  const [count, setCount] = useState(1); // Input the memo depends on.
  const total = useMemo(() => count * 2, [count]); // Cache until count changes.
  const bump = useCallback(() => setCount((value) => value + 1), []); // Stable function identity.
  console.log("memo"); // Cached calculation.
  console.log("callback"); // Cached function.
  console.log("skip"); // Skip work when inputs are unchanged.
  console.log("measure"); // Measure before you memoize.
  console.log("not-every-function"); // Do not wrap every handler.
  return <button type="button" onClick={bump}>{total}</button>;
}

04

Guided challenge

Do not memoize by habit

The last log says always. Change it to not-every-function.

export default function App() {
  console.log("memo"); // Keep this log.
  console.log("callback"); // Keep this log.
  console.log("skip"); // Keep this log.
  console.log("measure"); // Keep this log.
  console.log("always"); // Wrong - change to not-every-function.
  return <p>always</p>;
}

EXPECTED CONSOLE OUTPUT

memo
callback
skip
measure
not-every-function

Hints 0/2

    05

    Free sandbox

    Make a variation, test an assumption, or break the example on purpose. This draft is saved separately.

    import { useState } from "react";
    export default function App() {
      const [count, setCount] = useState(0); // Plain state is often enough.
      return <button type="button" onClick={() => setCount(count + 1)}>{count}</button>; // No memo needed here.
    }
    
    
    06

    Quiz

    A quick check for understanding. Retry as often as you like; your learning path stays open.

    01When is useCallback worth it?
    02What should you do before adding useMemo?

    Ready to call this one understood?

    Mark this lesson complete to earn 200 XP. You can always revisit it.

    Sign in to save progress
    Next lesson