4
0 Comments

React useMemo vs lodash memoize (Day 83)

lodash is a super useful package for data manipulation. Especially with nested objects, get, set, and deepClone are essential helpers to update complicated objects.

lodash memoize is also frequently used to cache expensive calls or calculations. But there is a big catch you need to be aware of: lodash memoize caches only the 1st argument. See how the memoized sum for 1 + 2 and 1 + 3 both return 3 in the screencap above.

This design limits the lodash memoize use case for key-value caching only. If you want to cache expensive calls with multiple variables, you can now do it with React Hooks useMemo. See the example from the official React Hooks documentation:

const memoizedValue = useMemo(
  () => computeExpensiveValue(a, b),
  [a, b]
);

Similar to useEffect, you can specify the variables you need to monitor. This design allows more flexible memoize uses.

posted to Icon for group Developers
Developers
on June 26, 2020
Trending on Indie Hackers
How we got 100+ clients in 48 hours from Product Hunt User Avatar 16 comments Meme marketing for startups 🔥 User Avatar 11 comments After 19,314 lines of code, i'm shutting down my project User Avatar 1 comment Need feedback for my product. User Avatar 1 comment We are live on Product Hunt User Avatar 1 comment Don't be a Jerk. Use this Tip Calculator. User Avatar 1 comment