ASP.NET Performance tip- Combine multiple script file into one request with script manager

Posted by Jalpesh P. Vadgama on ASP.net Weblogs See other posts from ASP.net Weblogs or by Jalpesh P. Vadgama
Published on Thu, 30 Dec 2010 05:52:46 GMT Indexed on 2010/12/30 5:55 UTC
Read the original article Hit count: 678

We all need java script for our web application and we storing our JavaScript code in .js files. Now If we have more then .js file then our browser will create a new request for each .js file. Which is a little overhead in terms of performance. If you have very big enterprise application you will have so much over head for this. Asp.net Script Manager provides a feature to combine multiple JavaScript into one request but you must remember that this feature will be available only with .NET Framework 3.5 sp1 or higher versions. 

Let’s take a simple example. I am having two javascript files Jscrip1.js and Jscript2.js both are having separate functions.

//Jscript1.js
function Task1() {
    alert('task1');
    }

Here is another one for another file.

////Jscript1.js
function Task2() {
    alert('task2');
}

Now I am adding script reference with script manager and using this function in my code like this.

 <form id="form1" runat="server">
        <asp:ScriptManager ID="myScriptManager" runat="server" >
            <Scripts>
                <asp:ScriptReference Path="~/JScript1.js" />
                <asp:ScriptReference Path="~/JScript2.js" />
            </Scripts>
        </asp:ScriptManager>
        <script language="javascript" type="text/javascript">
            Task1();
            Task2();           
        </script>
    </form>
Now Let’s test in Firefox with Lori plug-in which will show you how many request are made for this. Here is output of that. You can see 5 Requests are there.LoriPlugin

Now let’s do same thing in with ASP.NET Script Manager combined script feature. Like following

<form id="form1" runat="server">
        <asp:ScriptManager ID="myScriptManager" runat="server" >
            <CompositeScript>
                <Scripts>
                    <asp:ScriptReference Path="~/JScript1.js" />
                    <asp:ScriptReference Path="~/JScript2.js" />
                </Scripts>
                
            </CompositeScript>
        </asp:ScriptManager>
        <script language="javascript" type="text/javascript">
            Task1();
            Task2();           
        </script>
    </form>

Now let’s run it and let’s see how many request are there like following.

LoriPlugin1

As you can see now we have only 4 request compare to 5 request earlier. So script manager combined multiple script into one request. So if you have lots of javascript files you can save your loading time with this with combining multiple script files into one request. Hope you liked it. Stay tuned for more!!!.. Happy programming..

Shout it

© ASP.net Weblogs or respective owner

Related posts about ASP.NET

Related posts about c#.net