SOURCE

function fibonacci1(n) {
    if (n < 0) {
        throw new Error('参数必须大于0')
    }
    if (n === 1 || n === 2) {
        return 1
    }

    return fibonacci1(n - 1) + fibonacci1(n - 2)
}

console.time('fibonacci1')
console.log(fibonacci1(10))
console.timeEnd('fibonacci1')

function fibonacci2(n) {
    const arr = [0, 1, 1]

    function calc(n) {
        if (n < 0) {
            throw new Error('参数必须大于0')
        }
        
        if (arr[n] !== undefined) {
            return arr[n]
        }

        const data = calc(n - 1) + calc(n - 2)
        arr[n] = data

        return data
    }

    return calc(n)
}

console.time('fibonacci2')
console.log(fibonacci2(100))
console.timeEnd('fibonacci2')
console 命令行工具 X clear

                    
>
console