[thelist] ASP String Concatenation vs. Response.Write

Warden, Matt mwarden at odyssey-design.com
Mon Feb 26 15:38:37 CST 2001


> This topic came up very recently -- whether to concatenate ASP
> output into a string or to do consecutive Response.Writes (and if I'm
> duplicating previously stated information, I apologize, but I don't think I
> am). My understanding -- and I think the general consensus -- has been that
> doing lots of consecutive Response.Writes is bad because it forces
> context-switching (or some such thing) by the ASP engine,

To clarify, context-switching is this:

<%
' code
%>
other stuff
<%
'code
%>
other stuff

where the ASP engine has to turn the script parser on and off with every <%
and %> respectively. Generally a bad thing unless the 'code and "other stuff"
is quite large bits of code/other stuff  =)

> and that
> concatenating the output into a single string is preferable.
> An article on ASP101.com
> (http://www.asp101.com/articles/john/outputperf/default.asp) strongly
> suggests otherwise. The concatenation of numerous strings is such a slow
> process, relatively speaking, that it is actually *much* faster --
> especially when outputting large chunks of information -- to constantly
> Response.Write than to concatenate.

As I understand it, it is true that concatenation is slower. But, it depends
on exactly what you're talking about. For example:

Response.Write "hello"
Response.Write "world"

is faster than

strOut = "hellow"
strOut = strOut & "world"
Response.Write strOut

but not, of course, faster than

Response.Write "hello world"

The "duh!" factor is less when you have a larger example, but I'm lazy. Use
your imagination.

> Granted, probably some mix of Response.Write and string
> concatenation would be reasonable in most cases, but this article seems to
> prove that string concatenation is a very slow alternative even to
> consecutive Response.Writes.
> Comments?

Well, "very slow" might be a little over doing it, but generally concatenating
is not the thing to do unless you have a reason (other than performance) to do
it. Such as:

<plug>
ASP Application-level HTML Caching
http://evolt.org/article/ASP_Application_level_HTML_Caching/17/621/index.html
</plug>


--
mattwarden
mattwarden.com





More information about the thelist mailing list