wiki/content/20201126101955-memoization.md

34 lines
445 B
Markdown
Raw Normal View History

2024-05-06 20:40:05 +00:00
---
2024-10-29 18:27:12 +00:00
date: 2020-11-26
2024-05-06 20:40:05 +00:00
id: fdef5507-41d5-4510-b392-c1534f3def91
title: memoization
---
# Origin
Latin for "mindful remembering"
# Description
Cache the return value of a function
# Example
``` javascript
const memo = {}
function fib(n) {
if(memo[n]) {
return memo[n]
}
if(n <= 1) {
return 1
}
return memo[n] = fib(n -1) + fib(n - 2)
}
fib(5)
console.log(memo) // { '2': 2, '3': 3, '4': 5, '5': 8 }
```