Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet HTML/CSS Java JavaScript Node.js Python Python 3.11, Pandas, SQL
      Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. React
  4. Mastering React Performance Optimization: A Deep Dive into useMemo, useCallback, and memo

Mastering React Performance Optimization: A Deep Dive into useMemo, useCallback, and memo

Date- Apr 03,2026 1
react performance

Overview

React performance optimization is a critical aspect of developing scalable applications. As applications grow in complexity, the number of components and their interactions can lead to performance bottlenecks. React provides several tools, such as useMemo, useCallback, and memo, to help developers manage rendering behavior and minimize unnecessary re-renders.

The primary problem these tools address is the excessive rendering of components that may not need to update. This can happen due to new props or state changes that do not affect the component's output. By leveraging these optimization techniques, developers can ensure that their applications remain responsive and efficient, even as they scale.

Real-world use cases include applications with complex data visualization, large lists, or frequent state updates where performance can significantly impact user experience. For instance, a dashboard displaying real-time analytics would benefit from these optimizations to maintain smooth interactions.

Prerequisites

  • Basic understanding of React: Familiarity with React components, state, and props is essential.
  • Knowledge of React Hooks: Understanding how hooks like useState and useEffect work will provide context for useMemo and useCallback.
  • JavaScript fundamentals: A solid grasp of JavaScript, especially ES6 features, is necessary for writing and understanding modern React code.

Understanding useMemo

The useMemo hook is used to memoize expensive computations in React. It takes a function and an array of dependencies as arguments, returning a memoized value. This means that React will only re-compute the value when one of the dependencies changes, allowing for performance gains by avoiding costly calculations on every render.

Consider a scenario where you have a component that performs a complex calculation based on props or state. Without useMemo, this calculation would run on every render, potentially slowing down the application. By using useMemo, you can optimize this process.

import React, { useState, useMemo } from 'react';

const ExpensiveComponent = ({ number }) => {
  const computeFactorial = (n) => {
    return n <= 0 ? 1 : n * computeFactorial(n - 1);
  };

  const factorial = useMemo(() => computeFactorial(number), [number]);

  return 
Factorial of {number} is {factorial}
; }; const App = () => { const [count, setCount] = useState(0); const [number, setNumber] = useState(5); return (

Factorial Calculator

setNumber(Number(e.target.value))} />
); }; export default App;

In this code, the ExpensiveComponent calculates the factorial of a number passed as a prop. The computeFactorial function is an expensive operation. By wrapping it in useMemo, the factorial is only recalculated when the number prop changes. The count state does not affect the computation, so clicking the increment button does not cause the factorial to be recalculated, improving performance.

When to Use useMemo

It is essential to know when to use useMemo. While it can improve performance, using it indiscriminately can lead to unnecessary complexity and memory consumption. Ideal scenarios include:

  • Expensive calculations that depend on props or state.
  • Creating memoized objects or arrays that are passed to child components to prevent unnecessary re-renders.

Exploring useCallback

The useCallback hook is similar to useMemo but is specifically designed for memoizing functions. It takes a function and an array of dependencies, returning a memoized version of the function that only changes if one of the dependencies changes.

In React, passing functions down to child components can lead to unnecessary re-renders if the function is recreated on each render. By using useCallback, you can prevent this behavior, optimizing performance in components that rely on function props.

import React, { useState, useCallback } from 'react';

const ChildComponent = React.memo(({ onClick }) => {
  console.log('Child component re-rendered');
  return ;
});

const App = () => {
  const [count, setCount] = useState(0);

  const handleClick = useCallback(() => {
    console.log('Button clicked');
  }, []);

  return (
    

Count: {count}

); }; export default App;

In this example, the ChildComponent is wrapped in React.memo, which prevents it from re-rendering unless its props change. The click handler handleClick is memoized with useCallback, ensuring that it remains the same across renders. Thus, clicking the increment button does not cause the child component to re-render, improving overall performance.

When to Use useCallback

Like useMemo, useCallback should be used judiciously. Consider using it when:

  • You are passing callbacks to optimized child components that rely on reference equality to prevent re-renders.
  • Creating event handlers that are dependent on component state or props.

Using memo for Component Memoization

The memo function is a higher-order component that can prevent unnecessary re-renders of functional components. It does a shallow comparison of props and will only re-render the component if the props have changed.

This is particularly useful for components that receive complex objects as props or are expensive to render. By wrapping such components in memo, you can enhance performance by avoiding renders that do not lead to visual changes.

import React, { useState } from 'react';

const ExpensiveComponent = ({ data }) => {
  console.log('Expensive component rendered');
  return 
{data.name}
; }; const MemoizedExpensiveComponent = React.memo(ExpensiveComponent); const App = () => { const [count, setCount] = useState(0); const [data, setData] = useState({ name: 'Initial Data' }); return (

Count: {count}

); }; export default App;

In this code, the ExpensiveComponent is memoized using React.memo. The component only re-renders when the data prop changes. Pressing the increment button does not cause the expensive component to re-render, demonstrating effective memoization.

Custom Comparison Function with memo

By default, memo uses a shallow comparison for props. However, you can provide a custom comparison function to determine whether the component should re-render based on specific criteria.

const CustomMemoizedComponent = React.memo(ExpensiveComponent, (prevProps, nextProps) => {
  return prevProps.data.name === nextProps.data.name;
});

This allows for more granular control over the re-rendering behavior, which can be beneficial in specific scenarios where you need to optimize further.

Edge Cases & Gotchas

While using useMemo, useCallback, and memo can significantly enhance performance, there are common pitfalls to be aware of:

  • Overusing Memoization: Applying memoization indiscriminately can lead to increased memory usage and complexity. Only use these optimizations for expensive calculations or components that are frequently re-rendered.
  • Incorrect Dependencies: Failing to specify the correct dependencies in useMemo and useCallback can lead to stale values or unintended behavior. Always ensure that all variables that affect the result are included in the dependency array.
  • Shallow Comparison Limitations: React.memo performs a shallow comparison of props. If your props are complex objects, consider using a custom comparison function or ensuring that the objects are memoized as well.

Performance & Best Practices

To maximize performance when using these optimization techniques, follow these best practices:

  • Profile Your Application: Use React's built-in Profiler or tools like Chrome DevTools to identify performance bottlenecks before applying optimizations.
  • Measure Impact: Always measure the performance impact of your optimizations. Use performance metrics to quantify improvements and ensure that optimizations are effective.
  • Limit Use of Expensive Props: When passing props to memoized components, try to limit the number of props or ensure they are simple types. Complex objects can lead to performance degradation due to shallow comparisons.
  • Consider Component Structure: Sometimes, restructuring components to reduce the number of re-renders can be more effective than memoization.

Real-World Scenario: Building a Memoized Todo List

In this section, we will build a simple todo application that utilizes useMemo and useCallback for performance optimization. The application will allow users to add todos and filter them based on their completion status.

import React, { useState, useMemo, useCallback } from 'react';

const TodoItem = React.memo(({ todo, onToggle }) => {
  console.log(`Rendering Todo: ${todo.text}`);
  return (
    
  • onToggle(todo.id)} style={{ textDecoration: todo.completed ? 'line-through' : 'none' }}> {todo.text}
  • ); }); const TodoList = ({ todos, onToggle }) => { return
      {todos.map(todo => )}
    ; }; const App = () => { const [todos, setTodos] = useState([]); const [filter, setFilter] = useState('all'); const addTodo = (text) => { setTodos([...todos, { id: todos.length, text, completed: false }]); }; const toggleTodo = (id) => { setTodos(todos.map(todo => (todo.id === id ? { ...todo, completed: !todo.completed } : todo))); }; const filteredTodos = useMemo(() => { return filter === 'completed' ? todos.filter(todo => todo.completed) : filter === 'active' ? todos.filter(todo => !todo.completed) : todos; }, [todos, filter]); return (

    Todo List

    e.key === 'Enter' && addTodo(e.target.value)} placeholder="Add Todo" />
    ); }; export default App;

    This application allows users to add todos and filter them based on their completion status. The TodoItem component is memoized to prevent unnecessary re-renders. The filtered todos are computed using useMemo, so the filtering logic only runs when the todos or filter state changes.

    Conclusion

    • Understand the Purpose: useMemo, useCallback, and memo are essential for optimizing React applications and preventing unnecessary re-renders.
    • Use Judiciously: Apply these optimizations only when necessary to avoid unnecessary complexity.
    • Profile and Measure: Always measure performance before and after optimizations to ensure they have the desired effect.
    • Learn Next: Explore React's Concurrent Mode and Suspense for further performance improvements.

    S
    Shubham Saini
    Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
    View all posts →

    Related Articles

    Understanding Props and State in React: A Comprehensive Guide
    Apr 02, 2026
    Deep Dive into React Components: Understanding Functional and Class Components
    Apr 02, 2026
    Mastering Forms and Controlled Components in React: A Deep Dive
    Apr 02, 2026
    Mastering React Hooks: A Deep Dive into useState, useEffect, and useContext
    Apr 02, 2026
    Previous in React
    Fetching Data with Axios in React: A Comprehensive Guide
    Buy me a pizza

    Comments

    On this page

    🎯

    Interview Prep

    Ace your React interview with curated Q&As for all levels.

    View React Interview Q&As

    More in React

    • Mastering React Context API for Effective State Management 32 views
    • Mastering React: A Comprehensive Guide to Getting Started 14 views
    • Mastering Navigation in React with React Router 6 views
    • Fetching Data with Axios in React: A Comprehensive Guide 2 views
    • Mastering Redux with React: A Comprehensive Guide for Develo… 2 views
    View all React posts →

    Tags

    AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
    Free Download for Youtube Subscribers!

    First click on Subscribe Now and then subscribe the channel and come back here.
    Then Click on "Verify and Download" button for download link

    Subscribe Now | 1770
    Download
    Support Us....!

    Please Subscribe to support us

    Thank you for Downloading....!

    Please Subscribe to support us

    Continue with Downloading
    Be a Member
    Join Us On Whatsapp
    Code2Night

    A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

    Panipat, Haryana, India
    info@code2night.com
    Quick Links
    • Home
    • Blog Archive
    • Tutorials
    • About Us
    • Contact
    • Privacy Policy
    • Terms & Conditions
    • Guest Posts
    • SEO Analyzer
    Dev Tools
    • JSON Beautifier
    • HTML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • SQL Formatter
    • Diff Checker
    • Regex Tester
    • Markdown to HTML
    • Word Counter
    More Tools
    • Password Generator
    • QR Code Generator
    • Hash Generator
    • Base64 Encoder
    • JWT Decoder
    • UUID Generator
    • Image Converter
    • PNG to ICO
    • SEO Analyzer
    By Language
    • Angular
    • Angular js
    • ASP.NET
    • Asp.net Core
    • ASP.NET Core, C#
    • C
    • C#
    • C#, ASP.NET Core, Dapper
    • C#, ASP.NET Core, Dapper, Entity Framework
    • DotNet
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • Python 3.11, Pandas, SQL
    • Python 3.11, SQL
    • Python 3.11, SQLAlchemy
    • Python 3.11, SQLAlchemy, SQL
    • Python 3.11, SQLite
    • React
    • Security
    • SQL Server
    • TypeScript
    © 2026 Code2Night. All Rights Reserved.
    Made with for developers  |  Privacy  ·  Terms
    Translate Page
    We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
    Accessibility
    Text size
    High contrast
    Grayscale
    Dyslexia font
    Highlight links
    Pause animations
    Large cursor