Fibonacci JS

Benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
Linear
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)
  }
  return n2
}
myFunction(10)
ready
Exponent
function fibonacci2(n) {
  if (n <= 1) {
    return n
  }
  return fibonacci2(n - 1) + fibonacci2(n - 2)
}
fibonacci2(10)
ready
Linear Memonic
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.