Comparing Memorization in JS and useMemo in React: Performance Optimization Techniques

Zahra Teymouri
3 min readDec 28, 2023

--

Understanding the differences and similarities between memorization in JavaScript and useMemo in React can be enlightening for developers. Both concepts involve optimizing performance by caching values, but they serve different purposes in their respective environments.

Comparing Memorization in JS and useMemo in React: Performance Optimization Techniques

Memorization in JavaScript

In JavaScript, memorization refers to the technique of storing the results of expensive function calls and returning the cached result when the same inputs occur again. This helps in improving performance by avoiding redundant computations.

Consider a simple function to calculate the factorial of a number using memorization:

function memoizedFactorial() {
const cache = {};

return function factorial(num) {
if (num in cache) {
return cache[num];
} else {
if (num <= 1) {
return 1;
} else {
cache[num] = num * factorial(num - 1);
return cache[num];
}
}
};
}

const factorial = memoizedFactorial();

console.log(factorial(5)); // Output: 120 (calculated)
console.log(factorial(5)); // Output: 120 (retrieved from cache)

In this example, the function factorial caches the results for each computed factorial, avoiding unnecessary recalculations.

useMemo in React

useMemo is a hook in React that memoizes the result of a function and re-computes it only if the dependencies change. It is primarily used to optimize performance by avoiding unnecessary calculations in functional components.

Consider a React component that displays the square of a number using useMemo:

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

const SquareCalculator = () => {
const [number, setNumber] = useState(0);

const squaredNumber = useMemo(() => {
console.log('Calculating squared number...');
return number * number;
}, [number]);

return (
<div>
<input
type="number"
value={number}
onChange={(e) => setNumber(parseInt(e.target.value))}
/>
<p>Square: {squaredNumber}</p>
</div>
);
};

export default SquareCalculator;

In this example, the useMemo hook memoizes the computation of the squared number. The calculation only occurs when the number state changes, preventing unnecessary recalculations on re-renders triggered by unrelated state updates.

Differences:

  • Purpose: JavaScript memorization is a general technique used to cache function results for performance, while useMemo in React is specifically tailored for optimizing expensive computations within functional components.
  • Usage: Memorization in JavaScript involves manual caching within functions, whereas useMemo is a React hook that automatically handles memoization based on dependencies.

Similarities:

  • Performance Optimization: Both techniques aim to improve performance by avoiding redundant computations.
  • Caching: Both involve storing and retrieving cached values to prevent repeated calculations.

Understanding the differences and similarities between memorization in JavaScript and useMemo in React can empower developers to optimize performance effectively in different contexts.

Conclusion:

In summary, both memorization in JavaScript and useMemo in React focus on improving performance by caching values to avoid redundant computations.

The key differences lie in their purposes and usage: JavaScript memorization is a general technique applied within functions to manually cache results, while useMemo in React is a specific hook designed to memoize expensive computations within functional components based on dependencies. Despite their differences, both methods share the common goal of optimizing performance through efficient caching strategies.

--

--

Responses (1)