main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
function fiboRec(n) {
  return (
    n < 1 ? 0 :
    n === 1 ? 1 :
    fiboRec(n - 1) + fiboRec(n - 2)
  );
}

const fiboRecMemoized = (() => {
  const memo = new Map();
  const fiboRec = n => {
    let result = memo.get(n);
    if (result != undefined) return result;
    result = (
      n < 1 ? 0 :
      n === 1 ? 1 :
      fiboRec(n - 1) + fiboRec(n - 2)
    );
    memo.set(n, result);
    return result;
  }
  return fiboRec;
})();

console.time('fiboRec');
fiboRec(40);
console.timeEnd('fiboRec');

console.time('fiboRecMemoized');
fiboRecMemoized(40);
console.timeEnd('fiboRecMemoized');
Babel Compiler v6.4.4 Copyright (c) 2014-2015 Sebastian McKenzie