Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8172105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:40:47+00:00 2026-06-06T21:40:47+00:00

This code takes 3 seconds on Chrome and 6s on Firefox. If I write

  • 0

This code takes 3 seconds on Chrome and 6s on Firefox.
If I write the code in Java and run it under Java 7.0 it takes only 10ms.
Chrome’s JS engine is usually very fast. Why is it so slow here?
btw. this code is just for testing. I know it’s not very practical way to write a fibonacci function

fib = function(n) {
  if (n < 2) {
    return n;
  } else {
    return fib(n - 1) + fib(n - 2);
  }
};

console.log(fib(32));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T21:40:49+00:00Added an answer on June 6, 2026 at 9:40 pm

    This isn’t fault of javascript, but your algorithm. You’re recomputing same subproblems over and over again, and it gets worse when N is bigger. This is call graph for a single call:

                      F(32)
                   /         \
                F(31)            F(30)
              /     \           /      \
          F(30)     F(29)     F(29)    F(28)
        /  \      /     \     /   \     |    \
    F(29) F(28) F(28) F(27) F(28) F(27) F(27) F(26)
    
    ... deeper and deeper
    

    As you can see from this tree, you’re computing some fibonacci numbers several times, for example F(28) is computed 4 times. From the “Algorithm Design Manual” book:

    How much time does this algorithm take to compute F(n)? Since F(n+1)
    /F(n) ≈ φ = (1 + sqrt(5))/2 ≈ 1.61803, this means that F(n) > 1.6^n . Since our
    recursion tree has only 0 and 1 as leaves, summing up to such a large
    number means we must have at least 1.6^n leaves or procedure calls!
    This humble little program takes exponential time to run!

    You have to use memoization or build solution bottom up (i.e. small subproblems first).

    This solution uses memoization (thus, we’re computing each Fibonacci number only once):

    var cache = {};
    function fib(n) {
      if (!cache[n]) {
        cache[n] = (n < 2) ? n : fib(n - 1) + fib(n - 2);
      }
      return cache[n];
    }
    

    This one solves it bottom up:

    function fib(n) {
      if (n < 2) return n;
      var a = 0, b = 1;
      while (--n) {
        var t = a + b;
        a = b;
        b = t;
      }
      return b;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code, which i want to run, this takes a few mins so
//LINQPad version 4.31 - for me this code takes 1 minute and 40 seconds
In my computer this code takes 17 seconds (1000 millions times): static void Main(string[]
Problem Hello all! I have this code which takes my jpg image loops through
i found this code at activestate, it takes a string and prints permutations of
I have this small piece of code that basically takes a list and runs
I get this error: TypeError: object.__init__() takes no parameters when running my code, I
I would like to optimize the following code in R. This loop takes a
I am using this code taken from here : import smtplib def prompt(prompt): return
Take this code into consideration: Proxy p = new Proxy(Type.SOCKS, new InetSocketAddress(proxyURL, port)); try

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.