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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:18:36+00:00 2026-05-11T18:18:36+00:00

While doing some JavaScript performance tests I came up with the following piece of

  • 0

While doing some JavaScript performance tests I came up with the following piece of code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <title>Performance Test</title>
        <script>
            var time1;
            var time2;
            var executionTime;

            function writeALot(){
                time1 = new Date().getTime();
                var n = Number(document.getElementById("numberOfWrites").value);
                var strBuffer = "";
                document.getElementById("div1").innerHTML = "";

                for (var i = 1; i <= n; i++) {
                    strBuffer += i + "<br />";
                }

                document.getElementById("div1").innerHTML = "Number of Writes: " + n + "<br />";
                document.getElementById("div2").innerHTML = strBuffer;
            }

            function printExeTime(){
                time2 = new Date().getTime();
                executionTime = ((time2 - time1) / 1000).toString();
                document.getElementById("div1").innerHTML += "Execution Time: " + executionTime + " seconds";
            }

        </script>
    </head>
    <body>
        <div id="inputDiv">
            Number Of Writes
            <br/>
            <input type="text" id="numberOfWrites" value="10000">
            </input><input type="button" value="Go" onclick="writeALot();printExeTime()">
        </div>
        <br/>
        <div id="div1">
        </div>
        <div id="div2">
        </div>
    </body>
</html>

Basically what this does is count from 0 to the number in the text box (10000 default) concatenating each number and a “<br>” tag on a String, replaces the content of a Div with this String and shows the “time” I took to do the operation.

I did this to test how fast can generated content be printed on the document. While trying out this code I noticed couple of problems:

When you hit the go button for the first time, after a page refresh, the code runs a lot faster than the next attempts. For numbers less than 1000 after the second time the button is hit it might take like twice the amount of time it took to execute the same code for the first time, but once you start raising the number 100000 for example the difference in execution time get a lot bigger.

Try it yourself and see, load the page, hit the button once, code will execute fast, then hit the button again and see the diference? If the Same code is executed why does it takes so long after the first time?

The other thin I notices is that for some reason the way I measuring time is not working properly, seems like I’m getting a new date at the wrong moment, is there something like an event that’s fired after the content on a div is modified?

  • 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-11T18:18:37+00:00Added an answer on May 11, 2026 at 6:18 pm

    Comment out:

    // document.getElementById("div2").innerHTML = strBuffer;
    

    and see if you get more identical times.
    First time the amount of DOM manipulation over div2 is much simpler than any other time, because it has to:

    1. discard all div2 content
    2. create new content

    First time click doesn’t have step 1. And it also explains the difference when you have lots of elements compared to smaller number.

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

Sidebar

Related Questions

While looking through some code (javascript), I found this piece of code: <script>window.Bootloader &&
While debugging javascript written by someone else, I came across some code that I've
While doing some small regex task I came upon this problem. I have a
I'm doing some javascript development and found a cool macro to region my code
I am having problems with code written in ASP.NET with some javascript, doing a
Just saw some interesting code while doing a typo in coffee-script. I got the
While reviewing some of the code written in the Twitter Bootstrap Javascript, it looks
I'm doing some Javascript R&D and, while I've read Javascript: The Definitive Guide and
I was doing some tests on html 5 file upload with jquery and ran
While doing some random experimentation with a factorial program in C, Python and Scheme.

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.