What is Memoization in React ?
Last Updated :
05 Apr, 2024
Memoization is a powerful optimization technique used in React to improve the performance of applications by caching the results of expensive function calls and returning the cached result when the same inputs occur again. In this article, we will learn more about the concept of memoization and its application within React.
Introduction to Memoization
Memoization is a programming technique used to optimize the performance of functions by caching the results of expensive function calls and returning the cached result when the same inputs occur again. This technique is particularly useful in scenarios where the same computation is repeated with identical inputs, as it helps avoid redundant calculations and improves overall execution speed.
How Memoization Works ?
When a function is memoized, its return values are stored in a cache data structure, typically a hashmap or an associative array, where the function inputs serve as keys. Upon receiving new inputs, the function first checks if the result for those inputs is already cached. If a cached result exists, the function returns it directly without executing the computation again. If no cached result is found, the function computes the result, caches it, and then returns it.
In React, memoization plays a crucial role in optimizing rendering performance. Functional components can be memoized using the React.memo() higher-order component or the useMemo() hook, while class components can utilize PureComponent or shouldComponentUpdate() for memoization. By memoizing components, React ensures that they only re-render when their props or state change, preventing unnecessary re-renders and improving overall application responsiveness.
Method to demonstrate memoization
React.memo():
- React.memo() is a higher-order component provided by React that memoizes functional components.
- When wrapping a functional component with React.memo(), React memoizes the component's result. It re-renders the component only if its props have changed.
- This prevents unnecessary re-renders of components when their props remain the same, optimizing rendering performance.
Syntax:
const MemoizedComponent = React.memo(MyComponent);
useMemo() Hook:
- The useMemo() hook is used to memoize the result of expensive computations within functional components.
- It takes a function and an array of dependencies as arguments. The function is executed, and its return value is memoized. React re-executes the function only if any of the dependencies change.
- This is particularly useful for optimizing the rendering of components that perform expensive computations or calculations.
Syntax:
const memoizedValue = useMemo(() => computeExpensiveValue(dep1, dep2), [dep1, dep2]);
Benefits of Memoization
- Memoization optimizes performance by caching expensive function results.
- It conserves computational resources, reducing latency and improving efficiency.
- Memoized functions are more scalable, handling larger datasets or higher request volumes effectively.
- Memoization simplifies code by encapsulating caching logic within functions, promoting maintainability.
- In user-facing applications, memoization enhances responsiveness and reduces loading times.
- In React, memoization techniques like React.memo() and useMemo() lead to faster UI updates and smoother interactions.
Is Memorization always beneficial?
There are also some scenarios where Memorization isn't that much beneficial:
- Dynamic Data: Memoization may not be suitable for scenarios with frequently changing input data, leading to stale results.
- Large Cache Size: Maintaining a large cache can consume significant memory resources, potentially outweighing performance benefits.
- Complex Dependencies: Memoization may struggle with accurately defining dependencies in scenarios with complex data structures or functions, leading to incorrect caching.
- Garbage Collection Overhead: Caches used for memoization may accumulate unused entries over time, requiring careful management to avoid memory leaks.
- Performance Trade-offs: The overhead of caching and cache management may outweigh performance benefits in scenarios where computation costs are low.
- Concurrency and Thread Safety: Memoization introduces statefulness, potentially causing issues related to concurrency and thread safety in multi-threaded environments.
Implement React.memo() and useMemo
Step 1: Create a react application using the following command.
npx create-react-app <-foldername->
Step 2: Move to the Project folder
cd <-foldername->
Project Structure:

Dependencies:
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
Step 3: Create the required files as shown in folder structure and add the following codes.
JavaScript
//App.js
import React, { useState } from "react"
import ExpensiveComponent from './components/ExpensiveComponent'
import MemoizedExpensiveComponent from './components/MemoizedExpensiveComponent'
import MemoizedWithHookComponent from "./components/MemoizedWithHookComponent"
function App() {
const [inputValue, setInputValue] = useState(1);
const handleInputChange = (e) => {
setInputValue(parseInt(e.target.value));
};
return (
<div>
<h1>Memoization Examples</h1>
<label>
Input Value:
<input type="number" value={inputValue} onChange={handleInputChange} />
</label>
<h2>Without Memoization:</h2>
<ExpensiveComponent value={inputValue} />
<h2>With React.memo():</h2>
<MemoizedExpensiveComponent value={inputValue} />
<h2>With useMemo() Hook:</h2>
<MemoizedWithHookComponent value={inputValue} />
</div>
);
};
export default App;
JavaScript
//components/ExpensiveComponent.js
const computeExpensiveResult = (value) => {
console.log('Computing expensive result...');
let result = 0;
for (let i = 0; i < value; i++) {
result += Math.random();
}
return Math.ceil(result);
};
const ExpensiveComponent = ({ value }) => {
const expensiveResult = computeExpensiveResult(value);
return (
<div>
<p>Expensive Result: {expensiveResult}</p>
</div>
);
};
export default ExpensiveComponent;
JavaScript
//component/MemorizedWithHookComponent.js
import React, { useMemo } from 'react';
const computeExpensiveResult = (value) => {
console.log('Computing expensive result...');
let result = 0;
for (let i = 0; i < value; i++) {
result += Math.random();
}
return Math.ceil(result);
};
const MemoizedWithHookComponent = ({ value }) => {
const expensiveResult = useMemo(() => computeExpensiveResult(value), [value]);
return (
<div>
<p>Expensive Result: {expensiveResult}</p>
</div>
);
};
export default MemoizedWithHookComponent;
JavaScript
//components/MemorizeExpensiveComponent.js
import React from 'react';
const computeExpensiveResult = (value) => {
console.log('Computing expensive result...');
let result = 0;
for (let i = 0; i < value; i++) {
result += Math.random();
}
return Math.ceil(result);
};
const MemoizedExpensiveComponent = React.memo(({ value }) => {
const expensiveResult = computeExpensiveResult(value);
return (
<div>
<p>Expensive Result: {expensiveResult}</p>
</div>
);
});
export default MemoizedExpensiveComponent;
To start the application run the following command
npm start
Output:

Similar Reads
What is useState() in React ?
The useState() is a Hook that allows you to have state variables in functional components . so basically useState is the ability to encapsulate local state in a functional component. React has two types of components, one is class components which are ES6 classes that extend from React and the other
2 min read
What is React?
React JS is a free library for making websites look and feel cool. It's like a special helper for JavaScript. People from Facebook and other communities work together to keep it awesome and up-to-date. React is Developed by Facebook, React is a powerful JavaScript library used for building user inte
6 min read
What is a store in Redux ?
In Redux, the store is the central where all the state for the application is stored. This ensures the state is predictable and persistent for the entire application. is the central component for handling the state for the Redux application.Basics of Redux StoreThe Redux store is the heart of state
6 min read
What Are Props in React?
In React, props (short for "properties") are used to pass information from one component to another. The main purpose of props is to allow a parent component to send data to its child components.Here are some features of Props:Props cannot be modified by the receiving component.They are strictly for
6 min read
What is ComponentWillMount() method in ReactJS ?
ReactJS requires several components to represent a unit of logic for specific functionality. The componentWillMount lifecycle method is an ideal choice when it comes to updating business logic, app configuration updates, and API calls. PrerequisitesReact JSReact JS class componentsComponentWillMoun
4 min read
Lifting State Up in ReactJS
In ReactJS, "lifting state up" is a fundamental concept that plays an important role in managing the state efficiently across different components. When building React applications, it is common for multiple components to need access to the same piece of data or state. In such cases, instead of dupl
4 min read
What is react-router-dom ?
React Router DOM is an npm package that enables you to implement dynamic routing in a web app. It allows you to display pages and allow users to navigate them. It is a fully-featured client and server-side routing library for React. React Router Dom is used to build single-page applications i.e. app
6 min read
What are Action's creators in React Redux?
In React Redux, action creators are functions that create and return action objects. An action object is a plain JavaScript object that describes a change that should be made to the application's state. Action creators help organize and centralize the logic for creating these action objects.Action C
4 min read
How memoization optimizes performance in React ?
Have you ever wondered how computers can do complex tasks super fast? Well, memoization is like a secret trick that makes certain tasks much quicker by remembering past results instead of recalculating them every time. It's a game-changer for functions that do the same calculations over and over aga
6 min read
React Introduction
ReactJS is a component-based JavaScript library used to build dynamic and interactive user interfaces. It simplifies the creation of single-page applications (SPAs) with a focus on performance and maintainability.It is developed and maintained by Facebook.The latest version of React is React 19.Uses
8 min read