Giter Site home page Giter Site logo

node-fib's Issues

fewer dependencies

Perhaps some code like this with fewer dependencies (and less memory used) could work.
For 1000000 it returns Infinity, for 1000 it returns 2.686381002448534e+208 in 0.071s

function fibonacci(nfib, cb) {
  function fib(n1, n2, n, cb) {
    if(n > nfib) return cb(n2)
    process.nextTick(function() { fib(n2, n1 + n2, n + 1, cb) })
  }
  switch(nfib) {
    case 1: cb(0); break
    case 2: cb(1); break
    default: fib(0, 1, 3, cb)
  }
}

// usage example
// first arg is fibonacci number, 1-based
fibonacci(3, function(res) { console.log('fibonacci number is ', res) })

Cannot calculate 1,000,000th Fibonacci number

This crashes node-fib:

$ time curl localhost:3000/1000000
curl: (52) Empty reply from server

real    1m0.392s
user 0m0.006s
sys 0m0.004s

It appears it ran out of memory:

$ node app.js 
FATAL ERROR: CALL_AND_RETRY_2 Allocation failed - process out of memory

Should use closed-form fibonacci function

It would be much more performant to calculate the fibonacci number like so:

function fib (n) {
  var a=1
    , b=0
  while (0 > n--) {
    var c = b
    b = b + a
    a = c
  }
  return b
}

Perhaps it might make sense to add this as a separate endpoint?

Memoize results in an LRU cache

For optimum performance, the server should use node-lru-cache to memoize the results of each calculation in a lru cache. (npm install lru-cache is a pretty well tested O(1) implementation.)

Improve ten-fold by knowing math

O(log n)

phi = (1 + Math.sqrt(5)) / 2
fibonacci = (n) -> Math.floor(Math.pow(phi, n) / Math.sqrt(5) + 1/2)
fibonacci(10) # => 55

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.