Memoization
Memoization
Memoization is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and reusing those results when the same inputs occur again. It avoids repeated calculations by keeping a cache of input-output mappings, thereby improving performance, especially in computationally intensive applications.
Key Concepts of Memoization -
i) Cache Storage: Memoization involves maintaining a cache, which is a data structure (often a dictionary or object) that stores the results of function calls.ii) Function Inputs as Keys: The cache keys are the inputs to the function, and the values are the corresponding outputs.
iii) Avoiding Redundant Computation: When a function is called with the same inputs, the cached result is returned instead of re-computing the result.Benefits of Memoization -
i) Performance Improvement: Reduces the time complexity of recursive or iterative calculations by avoiding redundant computations.
ii) Optimized Resource Utilization: Saves computational resources by leveraging cached results.
iii) Enhanced React Performance: Prevents unnecessary re-renders and recalculations, leading to smoother user interfaces.
When to Use Memoization -
i) Expensive Calculations: Functions that perform computationally intensive tasks.ii) Frequent Function Calls: Functions that are called multiple times with the same inputs.
iii) React Components: Components that rely on props or state values that do not change frequently.React.memo
TheReact.memo
function is a higher-order component (HOC) that is used to optimize the performance of functional components in React by memoizing their output. As per the behavior, when the parent component gets re-render then its child component also gets re-rendered automatically.
When any child component is wrapped inReact.memo
, it will only re-render if its props have changed, thus preventing unnecessary re-renders and improving performance.
In case parent component re-rendered and if we used React.memo for child component then child component gets re-rendered only when there is any new props introduced. It will not re-rendered for the existing same props.How
React.memo
Works -
React.memo
performs a shallow comparison of the component's props. If the props remain the same between renders, the component will not re-render, saving processing time and resources.
To useReact.memo
, simply wrap your functional component with it.import React from 'react'; const MyComponent = (props) => { // Component logic return <div>{props.value}</div>; }; export default React.memo(MyComponent);
In this example,
MyComponent
will only re-render if thevalue
prop changes.Consider an example where you have a list of items and a child component that renders each item,
import React, { useState } from 'react'; const ListItem = React.memo(({ item }) => { console.log('Rendering:', item); return <div>{item}</div>; }); const ItemList = ({ items }) => { return ( <div> {items.map((item) => ( <ListItem key={item} item={item} /> ))} </div> ); }; const App = () => { const [items, setItems] = useState(['Item 1', 'Item 2', 'Item 3']); const [count, setCount] = useState(0); return ( <div> <button onClick={() => setCount(count + 1)}>Increment Count</button> <ItemList items={items} /> <div>Count: {count}</div> </div> ); }; export default App;
ListItem
is a memoized component. It will only re-render if theitem
prop changes.When you click the "Increment Count" button, the
App
component re-renders, butListItem
components do not re-render because their props (item
) have not changed.
Benefits ofReact.memo
i) Performance Optimization: By preventing unnecessary re-renders,React.memo
can significantly improve the performance of your application, especially in complex component trees.
ii)Ease of Use: It is easy to apply by simply wrapping your functional component, making it a quick way to optimize components.
iii) Flexibility: The option to provide a custom comparison function allows for more granular control over when re-renders should occur.
When to UseReact.memo
i) Pure Components: Components that rely solely on their props and do not use any state or context.
ii) Frequent Re-renders: Components that are rendered frequently and may not need to update if their props have not changed.
iii) Performance Bottlenecks: Parts of your application where re-renders are causing performance issues.
React.memo
is a powerful tool for optimizing the performance of functional components in React by memoizing their output and preventing unnecessary re-renders. By using it appropriately, you can enhance the efficiency and responsiveness of your React applications.useMemo hook
TheuseMemo
hook in React is used to memoize the result of a computation, ensuring that the computation is only performed when one of its dependencies has changed. This can be particularly useful for optimizing performance in functional components by avoiding expensive recalculations on every render.useMemo syntax is almost same as useEffect hook. Only the difference between useEffect and useMemo is that in useEffect we can't return any value but in useMemo we return the value.
HowuseMemo
Works -
TheuseMemo
hook takes two arguments,
i) A function: This function contains the computation whose result you want to memoize.
ii) An array of dependencies: The dependencies that the computation depends on. If any of these dependencies change, the function will be re-executed to compute a new value.
useMemo
returns the memoized value from the function, which will only be recalculated if one of the dependencies has changed.import React, { useMemo } from "react"; const memoizedValue = useMemo( () => { return expensiveCalculation(a, b); }, [a, b]);
computeExpensiveValue
is the function that performs the expensive calculation.
[a, b]
is the dependency array. If eithera
orb
changes, the function will be re-executed.
Example -import React, { useState, useMemo } from 'react'; const ExpensiveComponent = (props) => { const expensiveCalculation = (expensiveA, expensiveB) => { console.log('Calculating...'); // Simulate an expensive calculation return expensiveA + expensiveB; }; const memoizedValue = useMemo( () => { return expensiveCalculation(props.a, props.b); }, [props.a, props.b]); return <div>Result: {memoizedValue}</div>; } export default ExpensiveComponent; const App = () => { const [a, setA] = useState(1); const [b, setB] = useState(2); const [count, setCount] = useState(0); return ( <div> <button onClick={() => setA(a + 1)}>Increment A</button> <button onClick={() => setB(b + 1)}>Increment B</button> <button onClick={() => setCount(count + 1)}>Increment Count</button> <ExpensiveComponent a={a} b={b} /> <div>Count: {count}</div> </div> ); } export default App;
The
expensiveCalculation
function is wrapped inuseMemo
.
TheExpensiveComponent
component only recalculates the result whena
orb
changes.
Incrementing thecount
state does not trigger the expensive calculation, improving performance.
Benefits of useMemo -
i) Performance Optimization: Prevents unnecessary recalculations by caching the result of expensive computations.
ii) Efficiency: Helps avoid performance bottlenecks in components that perform heavy computations.
iii) Simplicity: Easy to use and integrate into existing components.
When to Use useMemo -
i) Expensive Calculations: When a component performs costly computations that do not need to be recalculated on every render.
ii) Frequent Re-renders: When a component re-renders frequently but the calculation results are only dependent on a subset of the component’s props or state.
iii) Stable Dependencies: When the dependencies for the calculation change infrequently, ensuring that memoization is effective.
TheuseMemo
hook is a valuable tool in React for optimizing performance by memoizing the results of expensive computations and ensuring they are only recomputed when necessary.useCallback hook
TheuseCallback
hook in React is used to memoize callback functions, preventing them from being recreated on every render. This can be particularly useful for optimizing performance, especially when passing callbacks to child components that rely on reference equality to avoid unnecessary re-renders.
HowuseCallback
Works -
TheuseCallback
hook takes two arguments,
A callback function: The function you want to memoize.
An array of dependencies: The dependencies that the callback depends on. If any of these dependencies change, the callback will be recreated.
useCallback
returns the memoized version of the callback function, which will only change if one of the dependencies has changed.const memoizedCallback = useCallback(() => { // Callback logic }, [dependency1, dependency2]);
() => { /* Callback logic */ }
is the callback function.
[dependency1, dependency2]
is the dependency array. If either dependency changes, the callback will be recreated.
Example -
Consider a component that increments a counter and has a button to trigger the increment,import React, { useState, useCallback } from 'react'; const IncrementButton = (props) => { return <button onClick={props.onClick}>Increment</button>; } const Counter = () => { const [count, setCount] = useState(0); const handleIncrement = useCallback(() => { setCount(count + 1); }, [count]); return ( <div> <p>Count: {count}</p> <IncrementButton onClick={handleIncrement} /> </div> ); } export default Counter;
handleIncrement
is memoized usinguseCallback
. It will only be recreated ifcount
changes.
TheIncrementButton
component receives the memoizedhandleIncrement
callback as a prop, which prevents it from re-rendering unnecessarily.
Benefits of useCallback -
i) Performance Optimization: Prevents unnecessary re-creations of callback functions, which can improve performance, especially in components that receive frequent updates.
ii) Stable References: Ensures that the reference to the callback function remains stable, which can be important for child components that rely on reference equality to avoid re-renders.
iii) Simplicity: Easy to use and integrate into existing components without significant changes.When to Use useCallback -
i) Passing Callbacks to Child Components: When you pass a callback function to a child component that relies on reference equality (e.g., usingReact.memo
).
ii) Avoiding Re-Creations: When you have callback functions that are recreated frequently due to parent component re-renders but do not need to be.
iii) Event Handlers: When you use callback functions as event handlers that should not be recreated on every render.Advantages of useMemo over React.memo
useMemo
andReact.memo
serve different purposes in optimizing React applications, and they complement each other rather than being directly comparable alternatives. However, there are specific scenarios whereuseMemo
offers advantages overReact.memo
.
i) Granular Control within Components
useMemo
allows you to optimize specific parts of a component by memoizing the results of expensive calculations or derived values. This level of granularity is not possible withReact.memo
, which only prevents the entire component from re-rendering.
ii) Handling Computations
If you have computationally expensive operations inside your component (e.g., filtering, sorting, or complex calculations),useMemo
can prevent these operations from being re-executed on every render, significantly improving performance.iii) Memoizing Non-Component Values
useMemo
is useful for memoizing any value or object, not just the output of a component. This is particularly helpful for avoiding unnecessary computations and maintaining referential equality of objects or arrays across renders.