DEV Community

Henri de la Hoz
Henri de la Hoz

Posted on

Fundamentals of useMemo

useMemo helps us to reduce the computing process in order to save memory. It applies memoization technique in order to speed the reply up by storing the result of a given function for given arguments.

We want to to be careful and ensure that the expected result of a function is going to be always the same for the same parameters.

If we want to use useMemo, the hook is very simple.

const filteredCryptos = useMemo(
() => cryptos.filter(
    (crypto) => crypto.name.toLowerCase().includes(search.toLowerCase())), 
[cryptos, search]);
Enter fullscreen mode Exit fullscreen mode

useMemo receives 2 parameters.

  • A closure function that will be the objective of the memoization.
  • an a array with 2 elements
    • the expected result of the function for the given parameter.
    • the parameter sent to the function for which we will sent the result without processing or executing the function.

Top comments (0)