Mahamudul Hasan Rubel
HomeBlogCoursesAboutProjectsSkillsExperiencePhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • Blog
  • Courses
  • About
  • Projects
  • Skills
  • Experience
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
Lesson 3 of the Intermediate React: Hooks, State & Data Patterns course
ReactJune 25, 20263 min read

Memoizing Expensive Calculations with useMemo for Performance

Learn how to use the useMemo hook to cache expensive calculations in React. Stop redundant re-renders and keep your dashboard UI fast and responsive.

Reactperformanceoptimizationmemoizationhooksfrontendjavascript

Previously in this course, we explored how to track mutable values and manage side effects using useRef in our persistent mutable values guide. While useRef helps us bypass the render cycle for certain data, sometimes we must compute data during render, but we want to avoid doing that work every single time.

In this lesson, we’ll tackle performance by learning how to cache complex data transformations using useMemo.

Why Performance Optimization Matters

In React, every time a component re-renders, every line of code inside the function body executes again. For simple UI updates, this is negligible. However, if your dashboard needs to process a large dataset—like filtering thousands of rows, sorting complex objects, or performing mathematical aggregations—re-running that logic on every keystroke or minor state change can make your interface stutter.

Before diving into code, it's vital to remember the lessons from our Performance Optimization Basics overview: never optimize prematurely. Use the React DevTools Profiler to confirm that a specific calculation is actually a bottleneck before applying useMemo.

Implementing useMemo

The useMemo hook allows you to "memoize" (cache) the result of a calculation. It takes two arguments: a factory function that returns the value you want to compute, and a dependency array.

React will only re-run the factory function if one of the dependencies in the array has changed since the last render. Otherwise, it returns the cached value from the previous render.

Worked Example: Filtering Dashboard Data

Imagine our dashboard displays a list of financial transactions. We allow the user to filter these by category using a dropdown.

JSX
import React, { useState, useMemo } from CE9178">'react';

function TransactionDashboard({ transactions }) {
  const [filter, setFilter] = useState(CE9178">'ALL');
  const [theme, setTheme] = useState(CE9178">'light');

  // Expensive operation: Filtering 10,000+ records
  const filteredTransactions = useMemo(() => {
    console.log("Filtering transactions...");
    return transactions.filter(t => 
      filter === CE9178">'ALL' ? true : t.category === filter
    );
  }, [transactions, filter]); // Only re-runs if transactions or filter change

  return (
    <div>
      <button onClick={() => setTheme(theme === CE9178">'light' ? CE9178">'dark' : CE9178">'light')}>
        Toggle Theme
      </button>
      {/* Rendering filteredTransactions... */}
    </div>
  );
}

In the example above, if the user toggles the theme, the TransactionDashboard component re-renders. Because transactions and filter haven't changed, useMemo skips the expensive .filter() call and returns the cached result immediately. Without useMemo, the application would perform that heavy filtering on every theme toggle.

Hands-on Exercise

For our running project, we have a DashboardMetrics component. Currently, it calculates the "Total Revenue" by summing up an array of thousands of individual invoice objects every time the user toggles the sidebar visibility.

  1. Open your DashboardMetrics.jsx.
  2. Locate the logic that sums the revenue.
  3. Wrap this calculation in a useMemo hook.
  4. Ensure the dependency array only includes the variables that actually affect the revenue calculation (e.g., the invoices array).
  5. Add a console.log inside the useMemo function to verify it only runs when the data actually changes, not when the sidebar toggles.

Common Pitfalls

While useMemo is powerful, it carries overhead. Here is how to avoid misusing it:

  • Over-memoization: Don't wrap every calculation in useMemo. Comparing dependencies and storing the result in memory costs resources. If the calculation is cheap (like simple arithmetic or string manipulation), the overhead of useMemo might actually make your code slower.
  • Missing Dependencies: If you use a variable inside your useMemo block but forget to add it to the dependency array, your UI will show stale data. Always use the eslint-plugin-react-hooks rule to catch these omissions.
  • Side Effects: Never place side effects (like API calls or DOM mutations) inside useMemo. That belongs in useEffect. useMemo is strictly for computing and returning values.

Recap

Performance optimization is about knowing when to skip work. By using useMemo, we protect our main thread from redundant calculations, ensuring our dashboard remains fluid even as our data grows. Remember: identify the bottleneck first, verify the cost, and then selectively apply memoization.

Up next: We'll look at useCallback, a specialized version of memoization for function references, which helps prevent unnecessary re-renders of child components.

Previous lessonPersistent Mutable Values with useRefNext lesson Optimizing Function References with useCallback
Back to Blog

Similar Posts

ReactJune 25, 20263 min read

Optimizing Function References with useCallback in React

Learn how to use useCallback to stabilize function identities, prevent unnecessary child re-renders, and master dependency arrays in your React components.

Read more
ReactJune 25, 20264 min read

Persistent Mutable Values with useRef: Managing React State

Learn to use useRef for persistent mutable values that don't trigger re-renders. Master tracking props and solving stale closures in your React projects.

Part of the course

Intermediate React: Hooks, State & Data Patterns

intermediate · Lesson 3 of 48

  1. 1

    Mastering useRef for DOM Access

    4 min
  2. 2

    Persistent Mutable Values with useRef

    4 min
  3. 3

    Memoizing Expensive Calculations with useMemo

    3 min
Read more
ReactJune 25, 20263 min read

Performance Optimization Basics: Mastering React Re-rendering

Performance optimization in React starts with understanding re-rendering. Learn how to identify bottlenecks and keep your UI responsive and fast.

Read more
  • 4

    Optimizing Function References with useCallback

    3 min
  • 5

    Introduction to Custom Hooks

    4 min
  • 6

    Building a useLocalStorage Hook

    4 min
  • 7

    Refactoring Dashboard Settings

    4 min
  • 8

    Complex State with useReducer

    3 min
  • 9

    Managing Object-Based State

    3 min
  • 10

    Introduction to Context API

    Coming soon
  • 11

    Architecting Global State with Context and Reducer

    Coming soon
  • 12

    Implementing Theme Context

    Coming soon
  • 13

    Structuring State for Performance

    Coming soon
  • 14

    Handling Authentication State

    Coming soon
  • 15

    Integrating Reducers with Auth State

    Coming soon
  • 16

    Introduction to React Router

    Coming soon
  • 17

    Dynamic Routing with URL Parameters

    Coming soon
  • 18

    Nested Routes and Layouts

    Coming soon
  • 19

    Protected Routes for Authenticated Views

    Coming soon
  • 20

    Programmatic Navigation

    Coming soon
  • 21

    Building the Dashboard Navigation Structure

    Coming soon
  • 22

    Asynchronous Data Lifecycle

    Coming soon
  • 23

    Caching Strategies with React Query

    Coming soon
  • 24

    Mutations and Data Updates

    Coming soon
  • 25

    Synchronizing Client and Server State

    Coming soon
  • 26

    Integrating Live Data into the Dashboard

    Coming soon
  • 27

    Error Handling and Loading UI

    Coming soon
  • 28

    Controlled vs Uncontrolled Components

    Coming soon
  • 29

    Real-time Form Validation

    Coming soon
  • 30

    Schema-based Validation with Zod

    Coming soon
  • 31

    Handling Multi-step Forms

    Coming soon
  • 32

    Optimizing Form Submissions

    Coming soon
  • 33

    Performance Profiling with React DevTools

    Coming soon
  • 34

    Refactoring for Scalability

    Coming soon
  • 35

    Finalizing Dashboard Data Flow

    Coming soon
  • 36

    Deploying the Application

    Coming soon
  • 37

    Advanced Hook Composition

    Coming soon
  • 38

    Implementing Middleware for State

    Coming soon
  • 39

    Advanced Context Patterns

    Coming soon
  • 40

    Router Loaders and Data Prefetching

    Coming soon
  • 41

    Complex Route Guards

    Coming soon
  • 42

    Handling Large Datasets in UI

    Coming soon
  • 43

    Testing Hooks and Components

    Coming soon
  • 44

    Managing Global Modals

    Coming soon
  • 45

    Implementing Keyboard Shortcuts

    Coming soon
  • 46

    Optimizing Asset Loading

    Coming soon
  • 47

    Internationalization Basics

    Coming soon
  • 48

    Managing WebSocket Connections

    Coming soon
  • View full course