views:

137

answers:

2

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"&gt;
<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?

A: 

Due to the thread model used by most javascript implementations, you will have a hard time doing what you are trying to do. The clock basically stands still while you do your tight loop. I do not have a suggestion for how to accurately time a loop like yours. Normally if I need to do something and update something while I do it, I use setTimeout or setInterval

A: 

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.

Robert Koritnik
This does work. now isn't it odd that refreshing the whole page executing the code again is actually faster than pressing the button twice? is there a faster way to erase/write on the div?
avanrotciv
I would try and set div2's "display" to "none" before updating it, so browser will save some time because it won't have to take care of the visual stuff behind the div2. I don't know whether this is going to speed things up or not, but it's worth a try. You could also try creating a new DOM node and copy content to it and discard div2 completely (also hiding it beforehand).PS: You could vote my answer up when accepting :)
Robert Koritnik
Oh forgot to say that checking the web what others have tried and compared the innerHTML vs. DOM manipulation in terms of speed could also give you some additional ideas what to try to speed it up. And depending on your application you could of course do "lazy loading" by loading the whole content part by part (check this page that does this to understand what I mean http://www.webresourcesdepot.com/dnspinger/). This way your page will be more responsive, smaller and users tend to work on a small subset of complete data at a time. Never as a whole.
Robert Koritnik