JavaScript Speed Test 3.0



I got an email from someone at Netscape. He made a valid point that by trying to write the computed values to the textarea after each loop I was really testing the browsers ability to use JavaScript to manipulate UI widgets, not the actual speed of the JavaScript interpreter. So, version 3.0 is the result. Now, I write the values just once after all the computations have been made. It makes a huge difference for most of the sluggish browsers in the previous tests.

BUT, the question still remains ... why is Netscape such a dog at changing the form field contents?

Start Time:

End Time:

Time Difference (seconds):

Computation Results:

  
letters = new Array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
colors  = new Array("red","blue","green","orange","purple","yellow","brown","black","white","gray");

function doTest()
{
   sob = document.forms[0].timeStart;
   eob = document.forms[0].timeEnd;
   dob = document.forms[0].timeDiff;
   rob = document.forms[0].results;

   // clear values
   sob.value = "";
   eob.value = "";
   dob.value = "";
   rob.value = "";
   endResult = "";

   nob   = new Date();
   start = nob.getTime();
   sob.value = nob.toUTCString();

   // do some math
   for (i=0;i<5000;i++)
   {
      tmp = Math.round(i*Math.SQRT1_2);
      addResult(tmp);
   }

   // do some strings and array stuff
   for (j=0;j<colors.length;j++)
   {
      fav = "My favorite color is " + colors[j] + ".\n";
      addResult(fav);

      favArray = fav.split(" ");
      for (l=0;l<favArray.length;l++)
      {
         addResult(favArray[l]);
      }
   }

   // do some more strings and array stuff
   for (k=0;k<5000;k++)
   {
      // make up email address
      name = makeName(8);
      email = name + "@mac.com";
      addResult(email);
   }

   rob.value = endResult;
   nob = new Date();
   end = nob.getTime();
   eob.value = nob.toUTCString();

   dob.value = (end - start)/1000;
}

function makeName(n)
{
   tmp = "";
   for (i=0;i<n;i++)
   {
      l = Math.floor(26*Math.random());
      tmp += letters[l];
   }
   return tmp;
}

function addResult(r)
{
   endResult += "\n" + r;
}
Was this page useful to you? Loading...