I’m editing some legacy code in classic ASP that uses a lot of code like the following:
pf "<div class=""span-6 last"" style=""margin-top:4px;"">"
pf "<div class=""clean-lightblue-left"" style=""margin-top:4px;"">"
pf "<span style=""float: left; margin-right: 0.3em;"" class=""ui-icon ui-icon-print""></span><a href=""#report"" name=""report"" id=""topic_report_download"">Topic Progress : <b>Download Report</b></a>"
pf "</div></div>"
In this example, pf is a utility function that is essentially the same as response.write with a line return.
I would rather do something like this:
...asp code
%>
Regular HTML code here
<%
asp code...
I’m new to asp, but I’ve heard of context-switching, and that it can be costly. Which of these two options is more costly?
First let me bottom line your specific example. A series of
Response.Writecalls just carrying plain string literals is going to be slower than including the complete HTML as content outside of the script code.This idea of “context switching” is a bit of a fallacy as a result of some unfortunate wording in some documentation (or perhaps history). We tend to think of an ASP file as being HTML with some script interspresed in it using
<% script here %>. In fact ASP file is a script file with some HTML in it. Its easier to reason on what its going if you think of it as having html content separated using%> content here <%(with an implied>%and<%at the start and end of the file).When the file is parsed to p-code a file entirely processable by the script engine is created (there is no “context switching”). In effect
%> .... <%becomes a special form of Response.Write that sends the content between the%><%to the buffer. The difference is that the bytes come verbatim as they appear in the original ASP file whereas the standardResponse.Writeaccepts a Unicode string from VBScript and then has to encode to theResponse.CodePagea set of bytes to put in the buffer.Hence the “context switching” is really just the number of times the script has to write to the buffer. Having many
<% = someExpression %>in your HTML creates a large number of buffer writes.These days unless you are generating an insanely large amount HTML (the article Doozer mentions is 10 years old) you would have buffering turned on. The cost of many writes is quite low. Reducing them can help but one has to remember that the way VBScript handles string contactenation is not without its problems too.