Fibonacci numbers in JavaScript

Find the nth element of the Fibonacci sequence.

In order of decreasing performance:

Iterative

An iterative Fibonacci function with linear time complexity.

function fibonacci(n) {
    if (n < 2) return n
    let a = 1, b = 1
    while (n > 2) {
        const t = a + b
        a = b
        b = t
        --n
    }
    return b
}

Recursive

A recursive Fibonacci function using memoization.

const cache = { 0: 0, 1: 1 }

function recursiveFibonacci(n) {
    if (!cache.hasOwnProperty(n))
        cache[n] = recursiveFibonacci(n - 1) + recursiveFibonacci(n - 2)
    return cache[n]
}