UNPKG

@onesy/algorithms

Version:
19 lines (18 loc) 685 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const cache = {}; function fibonacci(value, options = { cache: true }) { // Cache if ((options === null || options === void 0 ? void 0 : options.cache) && cache[value] !== undefined) return cache[value]; if (value < 2) return value; const values = [0, 1]; for (let i = 2; i <= value; i++) values[i] = values[i - 1] + values[i - 2]; const result = values[values.length - 1]; if ((options === null || options === void 0 ? void 0 : options.cache) && cache[value] === undefined) cache[value] = result; return result; } exports.default = fibonacci;