If you’re doing calculations that could take a lot of time, or if you are building off previous calculations a lot, storing that value in a cache could be helpful.
const fib = (n, memo) => {
memo = memo || {}
if (memo[n]) return memo[n]
if (n <= 1) return 1
return memo[n] = fib(n-1, memo) + fib(n-2, memo)
}
This is useMemo() in React.