by bascule · 15y ago · 0 comments
$ time curl localhost:3000/1000000
curl: (52) Empty reply from server
real 1m0.392s
user 0m0.006s
sys 0m0.004s
This actually crashes the server because it runs out of memory: $ node app.js
FATAL ERROR: CALL_AND_RETRY_2 Allocation failed - process out of memoryv8's current GC implementation has a hard memory limit of 1GB which is uppable to 1.7GB on 64bit systems. There's a new GC implementation in the works which is supposed to be able to beat this.
The memory usage is due to the overhead of nextTick creating a new stack each time, and the interpreter keeping track of where the callbacks need to lead. Splitting the calculation up into batches could be incorporated into the algorithm quite simply by adding an additional task to async.series which called fib(i) for i to n with a step of 10 when n is > 100.
Obviously there are much more efficient and effective ways of calculating the fibonacci sequence, but I quite like the recursive algorithm for its clearness and its amenability to memoisation.
You can up the limit by starting node with --max-old-space-size=<size in MB>