I was thinking about how can I optimize the markup of a page a bit. I found a great way (great but not very very very efficient) to do so. It is based on a strategy that came in my mind recently. Let's think a bit. We already have very interesting methods to cut bandwith of an ASP.NET page like for example compressing the view state and so on and so on. So provided we already have applied those techniques, if we still want to optimize what should we do? I call my method : iterative profit, you will soon understand why ... If we have something that repeats few times in the markup and is relatively long as name, if we make it shorter we should win some bytes right? And if the page is big enough and "the thing" is repeated more times, we |
So each time we cut the name of something, we win few bytes.
The next question is what can we cut in order to optimize our page?
What is the thing that repeats in each web control?
The answer is the "__doPostBack" function. I tried to replace it during the Rendering of the page and was able to cut a bit of the page size, while keeping the source relatively clean of errors (please note the word "relatively").
Here is a sample code:
protected override void Render(HtmlTextWriter writer)
{
System.IO.StringWriter stringWriter = new System.IO.StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
base.Render(htmlWriter);
string html = stringWriter.ToString();
html = html.Replace("\"javascript:__doPostBack", "\"__dpb");
writer.Write(html);
}
I had some issues I haven't yet identified, as I was in a rush to post this as it seems to me a brand new way to optimize pages - to look for repeating elements and cut their names... (I will be happy if there are volunteers to help with this one ;)...
Other things : DoPostBackWithOptions should also be replaced by dpbw for example, this will be done in the future, after I clean the _doPostBack as it still have few problems.
And some more things - we may consider optimizing some of the ASP.NET controls which are known to be "markup unoptimized" like the treeview, gridview for example (At least I think there may be some things in them to optimize).
Any comments are welcome.