Thursday, December 30, 2010

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

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
Share:

0 comments:

Post a Comment

Your feedback is very important to me. Please provide your feedback via putting comments.

Search This Blog
About Me
My Photo

Full Stack Developer,Blogger,Microsoft Mvp, Mentor, Life long learner, 10+ Years of Experience for JavaScript, web and Microsoft technologies. ASP.NET MVC,Web Forms,C#,AngularJs,Golang
Mvp profile
Subscribe to my blog

Enter your email address:

Follow us on facebook
Blog Archive
Total Pageviews