Fibonacci-perf

Benchmark created on


Description

Comparing Fibonacci algorithms: for loops and recursive ones.

Setup

const fibonacciForLoop = (n) => {
    let result = [];
    for (let i = 0; i < n; i++) {
        if (i === 0) {
            result.push(0);
        } else if (i === 1) {
            result.push(1);
        } else {
            result.push(result[i - 1] + result[i - 2]);
        }
    }
    return result.at(-1);
};

const fibonacciRecursive = function fib(n) {
    if (n === 0) {
        return 0;
    } else if (n === 1) {
        return 1;
    } else {
        return fib(n - 1) + fib(n - 2);
    }
};

Test runner

Ready to run.

Testing in
TestOps/sec
for
fibonacciForLoop(22);
ready
recursive
fibonacciRecursive(22);
ready

Revisions

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