Skip to content
Hou - Engineer & Tech Educator

Boost Your Frontend Performance with Memoized Async Functions

JavaScript, memo2 min read


Are you looking to optimize the performance of your frontend code? Do you have functions that take a long time to compute or involve I/O operations like network requests? Memoizing your async functions can be an effective solution to improve the responsiveness of your application and save valuable computation time.

What is memoization?

Memoization is a technique used to cache the result of an expensive function call so that subsequent calls with the same arguments can return the cached result instead of recalculating it. This approach can help to reduce the time it takes to load and execute your code, leading to a smoother and more efficient user experience.

Memoization is especially useful when you have functions that are computationally expensive or involve I/O operations like network requests. By caching the results of these operations, you can avoid repeating them unnecessarily and save valuable computation time.

Why/When to use memoized async functions?

As a frontend developer, you may encounter situations where memoization can be particularly useful. For example, if you have an app that needs to load a lot of data from an API, memoizing your async functions can help to reduce the number of requests and the amount of time it takes to load the data.

Memoizing async functions is important for maintaining responsiveness and reducing computation times. By caching the results of async function calls, you can ensure that your app responds quickly to user input and doesn't waste time repeating expensive operations.

How to use memoized async functions?

One approach to memoizing an async function is to use an object as a cache to store the results of previous calls. Here's an example implementation:

1const memoizeAsync = (fn) => {
2 const cache = {};
3 return async (...args) => {
4 const key = JSON.stringify(args);
5 if (cache[key]) {
6 return cache[key];
7 }
8 const result = fn(...args);
9 cache[key] = result;
10 return result;
11 };

In this example, we define a function called memoizeAsync that takes an async function fn as its argument. The function returns a new async function that memoizes the results of fn using an object as a cache.

The memoized function takes any number of arguments using the spread syntax (...args) and converts them to a string key using JSON.stringify(). If the cache already contains the key, the memoized function returns the cached value.

If the cache does not contain the key, the memoized function calls the original function fn with the given arguments and stores the result in the cache. Finally, the memoized function returns the result.

Here's an example of how you might use this function to memoize an async function that fetches data from an API:

1const fetchData = async (url) => {
2 const response = await fetch(url);
3 const data = await response.json();
4 return data;
7const memoizedFetchData = memoizeAsync(fetchData);
10 .then((data) => {
11 console.log(data);
12 })
13 .catch((error) => {
14 console.error(error);
15 });

In this example, we define an async function fetchData that fetches data from a URL and parses it as JSON. We then create a memoized version of this function using memoizeAsync.

When we call memoizedFetchData with a URL, the function will either return the cached result or fetch the data and store it in the cache. If an error occurs during the fetch, the memoized function will reject the promise and propagate the error.