views:

145

answers:

4

When we build our web apps we construct alot of HTML dynamically and the output them to placeholders at run time. One reason for this is to control our HTML output to optimise it for SEO and now that we have been doing this for a while its become 'the way we do it'. Generally its when we are looping through results.

My question is,

Is there any performance difference between alot of sb.append(someHTML) and then one placeholder.controls.add(new literalcontrol(sb.tostring) vs just using placeholder.controls.add(new literalcontrol(someHTML) every time.

Second should we be using some other method for building and reusing HTML snippets and injecting the data at runtime? We abandoned the standard .net data controls due to the code bloat.

Any thoughts?

A: 

Without knowing precisely what the "plc" object does, it's hard to say.

One way to find out is to use ASP.NET tracing, which gives you the time (in seconds) that it takes to render each element. You can enable this by placing

Trace="true"

in your <%@ %> header for the page.

Regarding the second part of your question, you may want to look at the ASP.NET MVC framework for new work. This is designed to do pretty much what you describe.

Jeremy McGee
A: 

If you're referring to the code bloat of those controls associated with webforms, you may want to look into the ASP.NET MVC framework. Although many "getting started" documents start out the projects from scratch, there are at least a couple that show how to start integrating it into an existing project.

Also, using existing controls, or at least specific user controls, will help you out a lot more as it keeps classes focused on their business value rather than mixing the business purpose with the "plumbing" code to build out your controls.

Agent_9191
Thanks for the tip on the MVC. We do split out our code into individual widgets but we still build alot of html in our code behind which constantly requires recompile to view changes. Not ideal but what we're doing for now. PS not sure why you were voted down. You're answering the second part of my question. Thanks.
Ben Jackson
A: 

Seen as one operation new is generally the slowest thing you can do based on that I'd say .Append is the fastest one of your suggestions. However as with all performance tuning you should testing using some kind of profiler.

Personally I use dotTrace from JetBrains

Rune FS
Good point on the 'new' that makes sense. Thanks for the tips on Tracing. We haven't really been doing any tracing on our code for optimisation more just a rough feel for 'does this page feel slow'.
Ben Jackson
A: 

I suspect that "it doesn't matter". Jeff Atwood's got a great post about micro optimizations: The Sad Tragedy of Micro-Optimization Theater (while not the same as what you're doing, it highlights the fact that spending time on certain "optimizations" just isn't worth it).

As others have already pointed out profile your code and the different methods and see the results for your application.

My gut feel is that you won't see a heck of a lot of difference between the various methods, unless you've got a very big control hierarchy or a whole bunch of text that you're manipulating.

I'd say the most important thing to do is ensure that you have a maintainable solution. One option would be writing your own server control that can be embedded in your SEO optimized pages.

dariom
Thanks Dariom, since I'm coding this every day it is more of 'which way should we go' rather than 'micro optimisations' which I agree with you on.
Ben Jackson