@onesy/algorithms
Version:
15 lines • 569 B
JavaScript
const cache = {};
export default function fibonacciRecursive(value) {
let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
cache: true
};
// Cache
if (options?.cache && cache[value] !== undefined) return cache[value];
if (value < 2) {
if (options?.cache && cache[value] === undefined) cache[value] = value;
return value;
}
const result = fibonacciRecursive(value - 1, options) + fibonacciRecursive(value - 2, options);
if (options?.cache && cache[value] === undefined) cache[value] = result;
return result;
}