Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Timing javascript

A little snippet that demonstrates how I perform speed tests against js code:

<html>
<
body>

<script language="javascript">

var Foos = { One:0, Two:0 } ;

Foos.Speak = function()
{
    return ( this.One + " : " + this.Two ) ;
}


function Bars()
{
    this.One = this.Two = 0 ;
}

Bars.prototype.Speak = function()
{
    return ( this.One + " : " + this.Two ) ;
}

bars = new Bars() ;



// Test
window.onload = function()
{
    // test 1
    var startTime=new Date() ;
    var s ;
    for( var i = 0; i < 200000; i++ )
    {
        Foos.Speak() ;
    }
    var endTime=new Date();
    document.getElementById("spnResult1").innerText = (endTime-startTime) ;


    // test 2
    startTime=new Date() ;
    for( var i = 0; i < 200000; i++ )
    {
        bars.Speak() ;
    }
    endTime=new Date();
    document.getElementById("spnResult2").innerText = (endTime-startTime) ;
}
script>

Foos: span id="spnResult1"> ms<br>
Bars: span id="spnResult2"> ms<br>

html>

1 Comment

Comments have been disabled for this content.