This module provides a function to compute the nth Fibonacci number using memoization to optimize performance.
Computes the nth Fibonacci number using memoization to optimize performance.
position(number): The position in the Fibonacci sequence to compute. If a non-negative integer is provided, it will default to 0.
number: The nth Fibonacci number.
import { fibonacci } from './index'
console.log(fibonacci(0)) // returns 0
console.log(fibonacci(1)) // returns 1
console.log(fibonacci(5)) // returns 5
console.log(fibonacci(10)) // returns 55- Import the
fibonaccifunction from the module. - Call the function with the desired position in the Fibonacci sequence.
- The function uses memoization to store previously computed Fibonacci numbers, which significantly improves performance for large values of
n. Retrieving a value in the nth position for the first time isO(2^n)(expensive), whereas after the initial compute it is memoized and becomes a simple array access forO(1)(fast).