When computing bigger Fibonacci numbers, Kendo UI performance concerns arise.

1 Answer 42 Views
Application
jhonson
Top achievements
Rank 1
Iron
Iron
jhonson asked on 22 Mar 2023, 11:04 AM
I'm attempting to use Kendo UI to implement the Fibonacci sequence, however, I'm having speed difficulties while computing higher Fibonacci numbers. Here's an illustration of my code:
function fibonacci(num) {
  if (num <= 1) {
    return 1;
  }
  return fibonacci(num - 1) + fibonacci(num - 2);
}

var series = [];
for (var i = 0; i < 10; i++) {
  series.push(fibonacci(i));
}
console.log(series);
I'm using a recursive function in this code to compute the Fibonacci sequence up to the tenth number and save the results in an array. When I try to calculate greater Fibonacci numbers, however, the performance of my code suffers. For example, if I update the loop to compute the first 50 Fibonacci numbers, the code runs slowly. 
I saw an article that suggested using an iterative method rather than a recursive approach. Is it going to work? Could someone please assist me?

1 Answer, 1 is accepted

Sort by
1
Accepted
Ivan Zhekov
Telerik team
answered on 23 Mar 2023, 12:53 PM

Hi, Johnson.

The main issue that you are facing is that each time you are calculating every next number, you need to calculate the preceding as well.

In the article that you referenced, it's quite elegantly illustrated with a picture, which speaks more than big O notation.

Considering that Fibonacci lists are deterministic i.e. constants, you can use that to your advantage. You can have a structure that holds the results and get them if they exist and calculate them and cache the result if it doesn't exists. Which is also one of the solutions offered in the article.

That way, once and only once, you will need to calculate numbers. Lookup is, for all intents and purposes, free or at least of negligible cost.

Here are two solutions:

The classic solution takes around 215 seconds for fib50, where as the caching solution takes about .17 ms or quite an improvement.

Lastly, I am not exactly sure how your question is related to Kendo UI, nor how you use Kendo UI to implement Fibonacci sequence, but here is the answer anyway.

Regards,
Ivan Zhekov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
Application
Asked by
jhonson
Top achievements
Rank 1
Iron
Iron
Answers by
Ivan Zhekov
Telerik team
Share this question
or