C:\aWork\UI\Perf\NbProj\web\index.jsp
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
    <meta http-equiv=Content-Type content="text/html; charset=windows-1251" />
    <title>Comparison of performance impact for anonymous functions with JavaScript
        variable versus local named functions.</title>
    <style  type="text/css">
    <!--
        @font-face      {font-family:Wingdings;}
        @font-face      {font-family:"Cambria Math";}
        @font-face      {font-family:Calibri;}
        h1, th, p, li, div
        {   margin-top:0in;
            margin-right:0in;
            margin-bottom:10.0pt;
            margin-left:0in;
            font-family:"Calibri","sans-serif";
        }
        .sample{ background-color: silver; font-family: "Courier New",Courier,monospace;}
    -->
    </style>
    <script  type="text/javascript">

        function $(id)
        {
            var el=document.getElementById(id);
            var ret = new function $init(){}
            ret.val=function(newVal)
            {   var ret = el.value;
                if( newVal )
                    el.value = newVal;
                return ret;
            }
            ret.html=function(newVal)
            {   if( !newVal )
                    return el.innerHTML;
                el.innerHTML = newVal;
                return this;
            }
            ret.attr=function(attr,newVal)
            {
                var ret = el.getAttribute(attr);
                if( newVal )
                    el.setAttribute(attr, newVal);
                return ret;
            }
            return ret;
        }
        function XhrGet( url )
        {
            var xhr = new XMLHttpRequest();
            xhr.open("GET", url, false);
            xhr.send(null);
            return xhr.responseText;
        }
        function T(){ return (new Date()).getTime(); }
        function SetSec(id,ms){ $(id).html( ms /1000.0); }
        function Browser()
        {   var n = window.navigator.userAgent;
            if( (/(Firefox[\/\s]\d+\.\d+)/.test(n)  )||
                ( /(MSIE[\/\s]\d+\.\d+)/.test(  n)  )||
                ( /(Chrome[\/\s]\d+\.\d+)/.test(n)  )||
                ( /(Safari[\/\s]\d+\.\d+)/.test(n)  )
              )return RegExp.$1;
            return n;
        }
        function ResetRealCount()
        {
            GblTotalFunctions += window.RealCount;
            $('TotalFunctions').html(GblTotalFunctions);
            window.RealCount = 0;
            SetSec('TotalTime' , T()-GblBeforeTime);
        }

    </script>
    <%
        final int MaxVariations=8;
        int Count = 1000;
        try{ Count = java.lang.Integer.parseInt( request.getParameter("Count") ); }
        catch( Exception ex ){}

        boolean Combinations[] = {true,true,true,true,true,true,true,true};
            if( request.getParameterValues("Comb") != null )
            {   for( int i=0;i<Combinations.length;i++)
                    Combinations[i]=false;
                for( String v: request.getParameterValues("Comb") )
                    try{ Combinations[ java.lang.Integer.parseInt( v ) ]=true; }catch( Exception ex ){}
            }
    %>
</head>

<body lang="EN-US" >

<h1>Comparison of performance impact for anonymous functions
    with JavaScript variable versus local named functions.
</h1>
    <form action="" >
    <table  border=1 cellspacing=0 cellpadding=3 style='border-collapse:collapse;border:none'>
        <tr>
            <th>#</th>
            <th>On</th>
            <th>Count</th>
            <th title="Variable or Func &#xA; Half or Each &#xA; Global or Local "
                >Var|Half|Ggl   </th>
            <th title="Full=render+run time"
                >full,<sub>     sec     </sub>  </th>
            <th title="JS run time w/o load"
                >run,<sub>      sec     </sub>  </th>
            <th>eval,<sub>      sec     </sub>  </th>
            <th>file size,<sub> bytes   </sub>  </th>
            <th>render,<sub>    sec     </sub>  </th>
            <th>Browser     </th>
            <th>Urls        </th>
        </tr>
<%
    for( int i=0;i<MaxVariations;i++ )
    {
        String H=(i&1)==1?"Half":"Each";// Half
        String V=(i&2)==2?"Var":"Fun";// UseVal
        String G=(i&4)==4?"Global":"Local";// Gbl

%>
        <tr>
            <td><%=i%></td>
            <td><input type="checkbox" name="Comb" value="<%=i%>" <%=Combinations[i]?"checked='checked'":""%>   /></td>
            <td id="Scr<%=i%>Count" >waiting...</td>
            <td id="Scr<%=i%>Comb"  class="sample"  ><%=""+H+" "+V+" "+G%></td>
            <td id="Scr<%=i%>Full"  >?</td>
            <td id="Scr<%=i%>Run"   >?</td>
            <td id="Scr<%=i%>Eval"  >?</td>
            <td id="Scr<%=i%>Size"  >?</td>
            <td id="Scr<%=i%>Render"    >?</td>
            <td id="Scr<%=i%>Browser"   >?</td>
            <td><a id="Scr<%=i%>Urls" href="AnonymousJsVsNamed.jsp">script</a></td>
        </tr>
<%
    }
%>
        <tr>
            <th colspan="4" >total functions &gt;</th>
            <td colspan="7" align="left" id="TotalFunctions">10</td>
        </tr>
        <tr>
            <th  colspan="4" >total time,<sub>      sec     </sub>  </th>
            <td colspan="7" align="left" id="TotalTime">0</td>
        </tr>
    </table>
    <input name="Count" id="Count" value="<%=Count%>"/>
    <input type="submit" value="reload" />
</form>


<p >Goal is to compare timing on similar set of functions(named and anonymous)  for:</p>
<ul>
    <li>Load time: JS load and compile, no execution. <br/>
        What is the impact of smaller footprint for named functions?</li>
    <li>Execution time of
        <ul>
            <li><b>Var</b> as reference to anonymous function:

            </li>
            <li>Full set: all functions are called</li>
            <li>Half set: initialize all, but run each even function to see the promise of smaller load time for uninitialized variables.</li>
            <li></li>
        </ul>
    </li>
</ul>
<p >Additional requirements:</p>
<ul>
    <li>Display file size for each test</li>
    <li>Show backend render time</li>
    <li>Run test on various browsers, collect results on public blog <br/>
        <a href="http://blog.firsov.net/2010/01/js-local-functions-anonymous-vs-named.html"
            >http://blog.firsov.net/2010/01/js-local-functions-anonymous-vs-named.html</a>
    </li>
</ul>
<!--
-->
<div id="ScriptsContainer" >

    <script  type="text/javascript">
        var GblBeforeTime=T();
        var GblTotalFunctions=0;
//      $('TotalFunctions').html(<%=Count*(MaxVariations+1)%>);

        var beforeTime= 0;// before JS tag
        var beginTime= 0;// to be set in JS on 1st line
        var afterTime = 0;// afrer JS tag
        
        var script    ="";// XHR of JS from tag src
        var RealCount = 0;// to be set by last JS function call
        var SkipEachOdd=false;


/** first load JS w/o stats to avoid server JSP pregenerating timing ******************************/
            var BASE = "Scr0";
    </script>
    <script id="Scr0" type="text/javascript" src="AnonymousJsVsNamed.jsp?BASE=SrcZ&Count=<%=Count%>" ></script>
    <script  type="text/javascript">

        ResetRealCount();

<%
    for( int i=0;i<MaxVariations;i++ )
    {
        if( !Combinations[i] )
            continue;
        
        //Variable or Func &#xA; Half or Each &#xA; Global or Local
        String H=(i&1)==1?"Half":"Each";// Half
        String V=(i&2)==2?"Var":"Fun";// UseVal
        String G=(i&4)==4?"Global":"Local";// Gbl
        String BASE = "Scr"+i;
        String url = "AnonymousJsVsNamed.jsp?BASE="+BASE+"&Count="+Count+"&UseVar="+V+"&Global="+G+"&Half="+H;
%>
/********************************/

            $('<%=BASE%>Count'  ).html('Loading...');
            $('<%=BASE%>Browser').html( Browser() );
            $('<%=BASE%>Comb'   ).html( "<%=""+H+" "+V+" "+G%>" );

            SkipEachOdd=<%="Half".equals(H) %>;
            beforeTime=T();
        </script>
        <script id="<%=BASE%>" type="text/javascript" src="<%=url%>" ></script>
        <script  type="text/javascript">
            afterTime=T();
            $('<%=BASE%>Count'  ).html( RealCount );
            SetSec('<%=BASE%>Full', afterTime-beforeTime);
            SetSec('<%=BASE%>Run' , afterTime-beginTime);
            SetSec('<%=BASE%>Render', beginTime-beforeTime );
            url = "<%=url%>";
            $('<%=BASE%>Urls').attr('href', url );
            script = XhrGet( url );
            $('<%=BASE%>Size'   ).html( script.length );

            ResetRealCount();
            beforeTime=T();
            eval(script);
            SetSec('<%=BASE%>Eval', T()-beforeTime );

            ResetRealCount();
<%
    }
%>

    </script>

</div>

<h4>Sources:</h4>
<a href="index.jsp.html">index.html</a> - this file |
<a href="AnonymousJsVsNamed.js.jsp.html">AnonymousJsVsNamed.js.jsp</a> - JS rendering JSP

<hr/>
<div style="text-align:right">
    <a href="#license"
        onclick="document.getElementById('BSDlicense').style.display='block';return true;">&copy; Sasha Firsov, 2010
    </a>
</div>
<pre id="BSDlicense" style="display:none">

<a name="license">BSD type licence.
Copyright (c) 2010, Sasha Firsov, Simulation Works, LLC
All rights reserved.
</a>
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
        this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
        this list of conditions and the following disclaimer in the
        documentation and/or other materials provided with the distribution.
    * Neither the name of the Simulation Works, LLC nor the names of its
        contributors may be used to endorse or promote products derived from
        this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
</pre>

</body>
</html>