Monthly Archives: July 2017

Expensive JavaScript with Fibonacci

Many times while working with JavaScript I encountered situations where it was useful to have an expensive script that would run for a certain time (like 5 to 10 seconds) thus blocking the JavaScript thread. This is helpful e.g. when you want to simulate expensive server-side actions.

Recently I came across this (in hindight obvious) solution in the excellent book Beginning Node.js by Basarat Ali Syed (Apress, 2014): An unoptimized fibonacci function.
I will leave this here for future reference.

function fibonacci(n) {
    if (n < 2)
        return 1;
        return fibonacci(n - 2) + fibonacci(n - 1);
fibonacci(44);    // modify this number based on your system performance