Fibonacci JS (v3)

Revision 3 of this benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
Linear (For Loop)
function myFunction(num) {
  let n1 = 0
  let n2 = 1

  for (let i = 0; i < num; i++) {
    let temp = n1 + n2
    n1 = n2
    n2 = temp
    console.log(n2)
  }
}
myFunction(10)
ready
LInear (While Loop)
function myFunction(num) {
  let n1 = 0
  let n2 = 1
  let temp
  while(num>=0){
    temp = n1
    n1 = n1+n2
    n2 = temp
    num--
  }
  return n2
}
myFunction(10)
ready
Recursive Exponent
function fibonacci2(n) {
  if (n <= 1) {
    return n
  }
  return fibonacci2(n - 1) + fibonacci2(n - 2)
}
fibonacci2(10)
ready
Linear memoization
function fibMemo(num, memo) {
  memo = memo || {}

  if (memo[num]) return memo[num]
  if (num <= 1) return 1
  return (memo[num] = fibMemo(num - 1, memo) + fibMemo(num - 2, memo))
}
fibMemo(10)
ready

Revisions

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