Fibonacci Algorithm

Benchmark published by System on

Description

Recursive vs Iterative vs Memoized.

Setup

function fibRec(n) { return n <= 1 ? n : fibRec(n-1) + fibRec(n-2); }
function fibIter(n) {
  let a = 0, b = 1, temp;
  for(let i=2; i<=n; i++) { temp = a + b; a = b; b = temp; }
  return n ? b : a;
}
const memo = {0: 0, 1: 1};
function fibMemo(n) {
  if (memo[n] !== undefined) return memo[n];
  return memo[n] = fibMemo(n-1) + fibMemo(n-2);
}
const target = 15;

Test Runner

Initializing...

Testing in
Test CaseOps/sec
Recursive
fibRec(target);
ready
Iterative
fibIter(target);
ready
Memoized
fibMemo(target);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

Revision 1
publishedby Systemon