fibonacci assignment
Memoize a function
HTML
<script src="https://www.ncbi.nlm.nih.gov/staff/maloneyc/javascript-tutorial/fibonacci-js/profiler.js"></script>
JavaScript
/*
Assignment: memo-ize this function so that it is
much faster when n gets large.
*/
const cache = [];
function fib(n) {
if (n in cache) {
return cache[n]
} else {
if (n <= 1) return n;
const result = fib(n-1) + fib(n-2);
cache[n] = result
return result}
}
profile(fib, [...Array(100).keys()]);