use_memo

use_memo is a hook to cache the result of a calculation, function, or operation. This is useful when you have a value that is expensive to compute and you want to avoid re-computing it on every render. The value is computed once and then stored in the memoized value. The memoized value is returned on subsequent renders until the dependencies change.

Example

In the example above, the filtered_todos value is computed once and then stored in the memoized value. The memoized value is returned on subsequent renders until the todos or filter dependencies change.

Recommendations

Recommendations for memoizing values:

  1. Use memoization for expensive computations: If you have a value that is expensive to compute, use use_memo to memoize the value between renders.
  2. Use dependencies: Pass in only the dependencies that the memoized value relies on. If any of the dependencies change, the memoized value is re-computed. If another prop or state value changes, the computation is not re-run.
  3. Do not use for cheap computations: There is a small overhead to memoizing values, so only use use_memo for expensive computations. If the value is cheap to compute, it is not worth memoizing.
  4. Memoize table/plot operations: If you are working with a table, memoize the table or plot operation, storing the result in a memoized value. This will prevent the table or plot from being re-computed on every render.

Optimizing performance

In the example below, we have a list of todos, a filter string, and a “theme” that can be set. First we demonstrate how slow it is to compute the filtered todos without memoization.

We are computing the filtered todos on every render, which is slow. We even call it when theme is changed, even though that value is not used in the computation. We can optimize this by using use_memo to memoize the filtered todos and only recompute them when one of the dependent values (todos and search) is updated:

Now, switching the theme will always be snappy, and the filtered todos will only be recomputed when the todos or search dependencies change.

Memoize table operations

use_memo can be used to memoize table operations. In the example below, we have a slider to adjust an integer value, a ticking table that will add a new row every second with the new result, and our theme picker from the previous example:

However, you’ll notice that if you change the theme, the table will be re-created and start again with no rows. We only want to re-create the table when n changes and we actually need to recreate the table. In that case, we can memoize the table operation:

Memoize each item in a list

use_memo is a hook, and like any other hook, can only be called at the top level of a component. Suppose you are listing several items, and you want them to be memoized individually. However, you can’t use use_memo, as an error is thrown if the count of hooks changes between renders:

Instead, extract each item into its own component and memoize the value there:

API Reference

Memoize the result of a function call. The function will only be called again if the dependencies change.

Returns: TypeVar(T) The memoized result of the function call.

ParametersTypeDefaultDescription
funcCallable[[], TypeVar(T)]The function to memoize.
dependenciesTuple[Any] |
List[Any]
The dependencies to check for changes.