Three quick ways optimize AJAX driven websites in ASP.NET

Recently I was involved in a project where I had to make heavy use of AJAX. I realized there are a few simple things you could do to improve performance.

1) Combine scripts

<ajaxToolkit:ToolkitScriptManager ID="TSM1" runat="Server"
CombineScriptsHandlerUrl="~/CombineScriptsHandler.ashx" />

As the name of the property suggests, it will pretty much combine all the needed JS files into one which in turn will reduce the number of requests sent to the server. You can find a detailed discussion about this here.

It is pretty easy to implement; instead of using the regular ScriptManager, just switch to the ToolkitScriptManager which comes with the AjaxToolkit and then set its CombineScriptsHandlerUrl property as shown above and throw the CombineScriptsHandler.ashx (included in the “SampleWebSite” directory of AjaxControlToolkit’s release package) into the root.

2) Run in release mode

The debug versions of the AJAX library have their source formatting preserved, as well as some debug asserts. By running it in release mode you can shave off some bytes off your requests.

<ajaxToolkit:ToolkitScriptManager ID="TSM1" runat="Server" 
EnablePartialRendering="true" ScriptMode="Release" />

Although, its important to note that some versions of Safari don’t seem to be compatible with this and could cause many strange side effects as this person and I have experienced in the past.

On a side note, ASP.NET AJAX Control Toolkit officially does not support Macs with PowerPC processors, its good to know that piece of information if a client ever demands an explanation as for why AJAX powered functionality seems to be broken or not functioning as expected in that environment.

3) Enable script caching and compression in web.config

    <scriptResourceHandler enableCompression="true"

This will compress and cache all the script files which are embedded as resources in an assembly, localization objects, and scripts that are served by the script resource handler.

But like the previous tip, there is a exception to this one too. Some versions of IE6 have a bug where they cant’t handle GZIP’d script files correctly. The RTM version of ASP.NET AJAX works around this by explicitly not compressing files for these versions of IE. Although if you are still having a problem, it just might be a safe bet to explicitly set the enableCompression property to false in the web.config.

If you have any questions or comments, please post them below. If you liked this post, you can share it with your followers or follow me on Twitter!