[알고리즘] 피보나치 메모이제이션 성능 확인




let cnt = 0;
function fib(n) {
    cnt++;
    console.log('called fib: ', cnt);
    return n<2 ? n : fib(n-1) + fib(n-2);
}
fib(10);
//called fib:  177

const memo = [0,1];
let count = 0;

function fibMemoization(n) {
  count++;
  console.log('called fibMemoization: ', count);
  if(typeof memo[n] !== 'number') {
      memo[n] = fibMemoization(n-1) + fibMemoization(n-2);
  }
  return memo[n];
}
fibMemoization(10);
//called fibMemoization:  19