1 import Benchmark from 'benny'
9 function fibonacciLoop (num) {
13 for (let i = 2; i <= num; i++) {
14 fib[i] = fib[i - 2] + fib[i - 1]
24 function fibonacciLoopWhile (num) {
39 function fibonacciRecursion (num) {
40 if (num <= 1) return num
41 return fibonacciRecursion(num - 1) + fibonacciRecursion(num - 2)
49 function fibonacciRecursionMemoization (num, memo) {
52 if (memo[num]) return memo[num]
53 if (num <= 1) return num
56 fibonacciRecursionMemoization(num - 1, memo) +
57 fibonacciRecursionMemoization(num - 2, memo))
61 `Fibonacci number ${number}`,
62 Benchmark.add('fibonacciLoop', () => {
65 Benchmark.add('fibonacciLoopWhile', () => {
66 fibonacciLoopWhile(number)
68 Benchmark.add('fibonacciRecursion', () => {
69 fibonacciRecursion(number)
71 Benchmark.add('fibonacciRecursionMemoization', () => {
72 fibonacciRecursionMemoization(number)
76 Benchmark.save({ file: 'fibonacci', format: 'json', details: true }),
77 Benchmark.save({ file: 'fibonacci', format: 'chart.html', details: true }),
78 Benchmark.save({ file: 'fibonacci', format: 'table.html', details: true })