var n = 8;
function getNthNumber() {
let prev = 0;
let afterPrev = 1;
let current = 1;
for(let i = 1; i <= n; i++) {
current = prev + afterPrev
afterPrev = prev
prev = current
}
return current
}
getNthNumber()
function getNthNumber() {
const memoize = [];
const cb = (n) => {
if(n <= 2 ) return 1;
if(memoize[n]) return memoize[n];
memoize[n] = cb(n - 1) + cb(n - 2);
return memoize[n];
}
return cb(n)
}
getNthNumber()
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Using Go Bottom Up | |
Using Memoization |
Test name | Executions per second |
---|---|
Using Go Bottom Up | 3338467.0 Ops/sec |
Using Memoization | 7016766.5 Ops/sec |
Benchmark Overview
The provided JSON represents a benchmark for comparing the performance of two approaches to calculate the nth Fibonacci number: "Go Bottom Up" and "Memoization". The benchmark is designed to test how efficiently each approach can compute Fibonacci numbers.
Options Compared
Two options are compared:
Pros and Cons
Library/Functionality
In this benchmark, no external libraries or functions are used besides JavaScript. The getNthNumber()
function is a custom implementation of the Fibonacci number calculation for each approach.
Special JS Features/Syntax
There are no special JavaScript features or syntax used in these benchmarks. They only utilize basic JavaScript constructs such as loops, variables, and functions.
Alternative Approaches
Other approaches to calculate the nth Fibonacci number include:
These alternative approaches may have different performance characteristics and use cases compared to the "Go Bottom Up" and "Memoization" approaches tested in this benchmark.