Monday, August 3, 2009

Loose weight of ASP.NET pages!!

If we don’t take care about how we feed our ASP .net pages… we will find that they will tend rapidly to gain too much weight, this can provide us a lot of headaches like:


  • Things go dam slow when moving into production (everything in localhost was running so smoothly…).

  • If you are using Partial Rendering (AJAX), your pages can become “not usable”, final users complaining about this.

  • Your server will have a hard time generating huge pages (I have seen ASP .net with a weight of 1 Mb or even 2 Mb.).

  • You can even flood your intranet network.

Does your web site generates pages that are over 100 K’s… even over 500 K’s… even over 1Mb… oh oh, you are in a trouble, We are going to do:


  • A fast action plan: just performing some small changes we can reduce the size of our page in-between the following range: 5% - 30 % (depends on the content that you are showing).

  • Other measures to reduce the size of the page, they require more work from your side but you can get quite good improvements.

First of all… How can I measure the weight of my page or partials posts? Download Fiddler, it’s a free sniffer that will let you check the size of your site requests / responses.

Ok, now we have the weighing machine, let’s start with the Action Plan:


  • Compilation: a thing that sometimes we forget and can make us add garbage in our pages; before compiling and deploying ensure that this setting is set to false (*):
                      
    <system.web>
    <compilation debug="false">

    (*) In your development environment you will need this set to true in order to debug.

  • Blank Spaces: in your development environment source code (ASPX) is indented using blank spaces, tabs… Do you really need that in your production environment? Just give a try, deploy your project and over that deployed pages (not your original source code) remove all the unnecessary blanks spaces on the left side… in some cases I have seen even an 20/ 30 % rate of improvement , and… if you even need to debug or edit a production web page, just when you open it with Visual Studio you will get it automatically reformatted(**). If you think this is not useful, just imagine a huge GridView with a lot of TR / TD… you really can save a loooot of weight there.

    (**) A good idea is to make a console application to trim spaces (give a folder, traverse subfolders and remove spaces on aspx, ascx…).

  • Master page: This one is lovely… find your contentplaceholder and change the id to one that is not more than two letters lenght… this can save you a lot of space, take into consideration this ID is repeated on every single control under it as a prefix(***).
                          
    <asp:ContentPlaceHolder runat="server" ID="phSidebar">

    Change it to:
                      
    <asp:ContentPlaceHolder runat="server" ID="sd">

    (***) You will have to perform a find/replace on all the old Id’s in the child pages (well and ensure that nobody had the bad idea of hardcode this ID’s in javascript instead of using CLIENTID).

  • Styles: If you are applying styles to GridViews and you are showing a big bunch of rows en sure that you apply them via CSS styles and using short names (every style definition is repeated on every single row / cell).

Now it’s time to upload your site and check with Fiddler the weight difference with your old pages.

Not good enough? Let’s go for plan B (a bit more of work, a bit more of performance):


  • ViewState:Remove the viewstate in your GridView (if possible in your whole page). ASP .net 2.0 incorporates ControlState, this makes easier to remove the viewstate on server controls (e.g. on a readonly GridView , if you remove the ViewState you will be still able to sort and perform paging). Take care with this, test well the controls where you remove the viewstate.

  • DOM: If some of the markup that you are going to show doesn’t need to be posted, just generating them on the client side via javascript.

  • Nonsense listboxes / combo's:Try to avoid having listboxes or other similar controls that hold more than 80 entries, this reduce the usability of the page and increases the weight, you can change it to an AJAX autocomplete extender, … if your client doesn’t approve that just try to generate the items via webservices + javascript.


Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

0 comments : on " Loose weight of ASP.NET pages!! "