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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:35:38+00:00 2026-05-27T00:35:38+00:00

Someone at work jokingly sent out an email with a html file intended to

  • 0

Someone at work jokingly sent out an email with a html file intended to crash your browser that was the following

<html>
<script type="text/javascript">
function crash(){
  for(i=0;i<5000000001;i++){
    document.write(i);
  }
}
</script>
<body onload="crash();">
</body>
</html>

Anyways it doesn’t do a great job of it in Chrome and a conversation arose that it created a friendly competition to see who could write javascript to make a page count to 5,000,000,000 as quickly as possible without causing the browser to become unresponsive or crash.

I came up with the following piece of javascript that is intended to be used in Chrome.

<html>
<script type="text/javascript">
function countToFiveBillion(counter, num){
  if(num < 5000000000)
  {
    num++;
    if(num % 18700 == 0){
      counter.innerHTML = num;
      setTimeout(function() {countToFiveBillion(counter, num)}, 1);
    } else {
      countToFiveBillion(counter, num);
    }
  }
}
function initiateCountDown()
{
   var counter = document.getElementById("counter");
   var num = +counter.innerHTML;
   countToFiveBillion(counter, num);
}
</script>
<body onload="initiateCountDown();">
<div id="counter">0</div>
</body>

</html>

The reason that this will only run in chrome is that I’m using the setTimeout call to avoid creating a stackoverflow in chrome. (Chrome also allows you the largest stack for recursive calls out of all of the browsers).

Is there any way for me to make this count any quicker? I think that I can increase the amount counted a little before it causes an overflow (somewhere less than 100 though) The only stipulation is that is has to display as many numbers as possible as it counts.


Improved Code:

<html>
<script type="text/javascript">
var counter;
var num = 0;
function countToFiveBillion(){
    if(num < 5000000000)
    {
    num++;
    if(num % 18701 == 0){
        setTimeout("countToFiveBillion()", 1);
            counter.value = num;
        } else {
        countToFiveBillion();
    }
    } else {
        counter.value = "number greater than 5 Billion";
    }
}
function initiateCountDown()
{
   counter = document.getElementById('counter');
   countToFiveBillion();
}
</script>
<body onload="initiateCountDown();">
    <input type="text" id="counter" value="0" />
</body>

</html>
  • Made count and element globabl
  • Switched to text input instead of div
  • moved update UI to after setting the callback
  • 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-27T00:35:39+00:00Added an answer on May 27, 2026 at 12:35 am

    Don’t use .innerHTML = ... to display the number. According to this test, setting the value property of an input element is more efficient.

    <input type="text" id="counter" value="0" />
    

    Instead of constructing a new function, I recommend to use global / local variables, and passing a function reference as an argument to setTimeout, or use setInterval at init.

    • Swap setTimeout("countToFiveBillion()",1) for setTimeout(countToFiveBillion,0).
      Explanation: "countToFiveBillion()" is inefficient; First, the string gets converted to a function and called, then another function call follows. The suggested function runs only has to call a function, without creating new ones. It’s also called a split second faster.
    • Lift the limit (I was able to increase 18701 to 20000). After lifting the limit to such a rounded number, I noticed that the counter value is updated between each time-out.
    • Fixed some errors in the implementation (replaced .innerHTML with .value at the else-block).

    Relevant code:

    <input type="text" id="counter" />
    <script>
    var counter, num = 0;
    function countToFiveBillion(){
        if(num < 5e9)
        {
            if(++num % 18701 == 0){
                setTimeout(countToFiveBillion, 0);
                counter.value = num;
            } else {
                countToFiveBillion();
            }
        } else {
            counter.value = "number greater than 5 Billion";
        }
    }
    function initiateCountDown(){
        counter = document.getElementById('counter');
        counter.value = num; //Init, show that the script is 
        countToFiveBillion();
    }
    window.onload = initiateCountDown;
    </script>
    

    Fiddle: http://jsfiddle.net/KTtae/

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

Sidebar

Related Questions

Today at work someone tried to convince me that: {$obj->getTableInfo()} is fine for smarty/mvc/templating
How do you work with someone when they haven't been able to see that
Can someone tell me please why the following example work in Firefox but not
I was wondering if someone could work this one out as I've been trying
Could someone please work out what's happening here, and how I could fix it?
Can someone help me work out just what I happening here. I have an
I have tried a solution that seems to work for someone else on this
Someone where i work noticed (in stacktrace) that when running the jvm with -javaagent:spring-instrumentation.jar
I have a Rails3 app that is based on someone else's work. For some
Someone at work just asked for the reasoning behind having to wrap a wait

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.