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 822435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:47:52+00:00 2026-05-15T02:47:52+00:00

Why the following code runs so…. slow……. ? <html><body><script type=text/javascript> var i = 0;

  • 0

Why the following code runs so…. slow……. ?

<html><body><script type="text/javascript">
    var i = 0;

    f();

    function f() {
        if (i == 5000) {
            document.write("Done");
        } else {
            i++;
            tail();
        }
    }

    function tail() {
        var fn = tail.caller;
        var args = arguments;
        setTimeout(function() {fn.apply(this, args)}, 0);
    };

</script></body></html>
  • 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-05-15T02:47:53+00:00Added an answer on May 15, 2026 at 2:47 am

    A couple of reasons:

    1. Using the caller property on a function object introduces overhead (and is non-standard)

    2. The same for using the arguments pseudo-array (IIRC, this will slow down a function call by a factor of 2-5, depending on which browser you’re using)

    3. setTimeout will typically take no less than 10ms to call your function back (although Chrome sometimes goes a bit faster than that), even when you specify 0 as the timeout. This will probably be the biggest reason: 5,000 iterations at ~10ms is 50 seconds.

    That last item is the reason I said:

    If you have a lot of loop iterations (e.g., a couple of hundred instead of 15), it may well be worth doing a chunk of them on each iteration rather than yielding on each iteration; the yield takes a measureable time (typically ~10-15ms).

    …in my answer to your other recent question.

    Note that your code can be rewritten without using caller or arguments, making it much clearer:

    var i = 0;
    
    f();
    
    function f() {
        if (i == 5000) {
            document.write("Done");
        } else {
            i++;
            setTimeout(f, 0);
        }
    }
    

    Separately: I notice you used document.write in your code sample. If you’re doing this with code executed during the page parse (which is the only place you can use document.write), rather than in code you trigger with an event handler after the page is parsed, you can’t use setTimeout. This is because code that ouputs via document.write has to run synchronously with the HTML parser on page load. Script blocks are executed during the parse for exactly this reason: In case they emit HTML the parser has to deal with. If you want to modify the page after parsing (which I’d recommend), you want to use DOM methods to get an element reference, and then either use DOM methods to add to it or (and this is both easier and faster running) set the innerHTML property on the element.

    My sense from both questions is that you probably do want to be looking into web workers, and only using the above for fallback for browsers that don’t support them yet (IE, I’m afraid, but I bet IE9 will have them — and all the other major ones have them now).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 437k
  • Answers 437k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can compute the path of the projectile y(x) by… May 15, 2026 at 4:18 pm
  • Editorial Team
    Editorial Team added an answer You can't declare and set an NSNumber like this: NSNumber… May 15, 2026 at 4:18 pm
  • Editorial Team
    Editorial Team added an answer You can see my answer working here: http://jsfiddle.net/kq2uQ/ HTML I… May 15, 2026 at 4:18 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.