As a software engineer by trade, I tend to think about things such as performance being a key factor to the quality of pretty much everything I use. If I go to a website and it takes me 30 seconds to load a very simplistic interface with a few paragraphs of text I tend to get an instant dislike for the application. I know that most users feel the same way. I know years ago in the age of dial up, it was said that if a user ever had to wait more than thirty seconds for a page to load, you had a 50% chance of losing that user on your site forever… My question is what happened to those days of optimization? Was it truly lost with the age of bandwidth? Do we really care about the fancy buttons that much that we've forgotten about writing quality interfaces?
When I recently had the pleasure of working through some old code written a couple of years back by another developer I had a very strange eye opening experience; just how inefficient Server controls can be in .net. This shouldn't be new to anyone really… I mean in the first couple of chapters of my MCTS book it very clearly illustrates that server controls can provide dynamic behavior, and greatly simplify the process of processing the user interaction state of development, but server controls should only be used when this is required. With this rather lengthy introduction, I present to you the most misused server control, the ASP Label.
Microsoft defines the Label Web Server control as having the functionality to "Display static text on a web forms page and allow you to manipulate it programmatically." I use labels all of the time for this purpose, and they work really nicely for it. This however is one of things I do not use a label for:
<asp:Label runat="server" ID="HeaderText" CssClass="H1HeaderText" Text="Static Header"/>
I would instead place something like:
<h1>
Static Header
</h1>
I know what you're thinking, what's the big deal with putting a few extra labels here and there? Well, I was wondering the exact same thing, so I did a bit of a test to prove my point.
I set up two pages, one with nothing but a label that had some text in it, and another with nothing but a span tag with the same text in it. I used a span tag because that is usually what a label will resolve to after being rendered by .net. I than set up a bit of a test using Watin to spin up several threads, and test the performance of rendering that page with a single label tag vs. the one without. After loading and closing each page 100 times, I found that on average it took roughly 100 ms more to load the page with a single label. I did the test again but this time placed two labels on the one page, and two equivalent span tags on the other. I found that it took roughly 200ms longer to load the page with two labels than the page without. That worked out to be roughly 10% of the total rendering time of the page, keeping in mind of course the only thing on the pages was a label.
So, 100ms per label… that's not that big of a deal right? Well imagine a page with 10 form elements on it, a few paragraphs of static text, and maybe a header and footer, this seems like a fairly typical form to me. Say we said that there were 20 labels total that could be converted to their HTML equivalent without losing any functionality. Given that my small experiment is correct, that would mean that 2 full seconds of server processor time could be simply cut, per request. That means that you could improve the efficiency of these pages by as much as 10%, for doing little more than using a control for its intended purpose… It is for that reason I consider the label ASP's most abused Server control.