· What is the most effective way to write ASP generated content to the response stream?
·
Should I enable buffering?
· Should I be concerned about adding comments to my ASP code?
· Should I explicitly set the default language for the page?
· Should I disable the Session State if it is not required?
· Should I place my script logic in sub and function blocks?
· What is the impact of using include files?
· How much overhead is imposed when implementing error handling?
· Is there a penalty for setting a transaction context?
<% OPTION EXPLICIT
Dim FirstName
Dim LastName
Dim MiddleInitial
Dim Address
Dim City
Dim State
Dim PhoneNumber
Dim FaxNumber
Dim EMail
Dim BirthDate
FirstName = "John"
MiddleInitial = "Q"
LastName = "Public"
Address = "100 Main Street"
City = "New York"
State = "NY"
PhoneNumber = "1-212-555-1234"
FaxNumber = "1-212-555-1234"
EMail = "john@public.com"
BirthDate = "1/1/1950"
%>
<html>
<head>
<title>Response Test</title>
</head>
<body>
<h1>Response Test</h1>
<table>
<tr><td><b>First Name:</b></td><td><%= FirstName %></td></tr>
<tr><td><b>Middle Initial:</b></td><td><%= MiddleInitial %></td></tr>
<tr><td><b>Last Name:</b></td><td><%= LastName %></td></tr>
<tr><td><b>Address:</b></td><td><%= Address %></td></tr>
<tr><td><b>City:</b></td><td><%= City %></td></tr>
<tr><td><b>State:</b></td><td><%= State %></td></tr>
<tr><td><b>Phone Number:</b></td><td><%= PhoneNumber %></td></tr>
<tr><td><b>Fax Number:</b></td><td><%= FaxNumber %></td></tr>
<tr><td><b>EMail:</b></td><td><%= EMail %></td></tr>
<tr><td><b>Birth Date:</b></td><td><%= BirthDate %></td></tr>
</table>
</body>
</html>
Complete code for /app1/response1.asp|
Previous Best: |
8.28 |
msec/page |
|
|
…
Response.Write("<html>")
Response.Write("<head>")
Response.Write(" <title>Response Test</title>")
Response.Write("</head>")
Response.Write("<body>")
Response.Write("<h1>Response Test</h1>")
Response.Write("<table>")
Response.Write("<tr><td><b>First Name:</b></td><td>" & FirstName & "</td></tr>")
Response.Write("<tr><td><b>Middle Initial:</b></td><td>" & MiddleInitial & "</td></tr>")
…
Snippet from /app1/response2.asp|
Previous Best: |
8.28 |
msec/page |
|
|
|
Response Time: |
8.08 |
msec/page |
|
|
|
Difference: |
-0.20 |
msec |
2.4% |
decrease |
…
writeCR("<tr><td><b>First Name:</b></td><td>" & FirstName & "</td></tr>")
…
SUB writeCR(str)
Response.Write(str & vbCRLF)
END SUB
Snippet from /app1/response4.asp|
Previous Best: |
8.08 |
msec/page |
|
|
|
Response Time: |
10.11 |
msec/page |
|
|
|
Difference: |
2.03 |
msec |
25.1% |
increase |
…
Response.Write("<html>" & _
"<head>" & _
"<title>Response Test</title>" & _
"</head>" & _
"<body>" & _
"<h1>Response Test</h1>" & _
"<table>" & _
"<tr><td><b>First Name:</b></td><td>" & FirstName & "</td></tr>" & _
…
"<tr><td><b>Birth Date:</b></td><td>" & BirthDate & "</td></tr>" & _
"</table>" & _
"</body>" & _
"</html>")
Snippet from /app1/response3.asp|
Previous Best: |
8.08 |
msec/page |
|
|
|
Response Time: |
7.05 |
msec/page |
|
|
|
Difference: |
-1.03 |
msec |
12.7% |
decrease |
…
Response.Write("<html>" & vbCRLF & _
"<head>" & vbCRLF & _
" <title>Response Test</title>" & vbCRLF & _
"</head>" & vbCRLF & _
…
Snippet from /app1/response5.asp|
Previous Best: |
7.05 |
msec/page |
|
|
|
Response Time: |
7.63 |
msec/page |
|
|
|
Difference: |
0.58 |
msec |
8.5% |
increase |
· Avoid excessive use of Inline ASP.
· Always concatenate consecutive Response.Write statements into a single statement.
· Never use wrapper functions around the Response.Write statement to append CRLFs.
· If you must format your HTML output, append CRLFs directly within the Response.Write statements.
<% OPTION EXPLICIT
Response.Buffer = true
Dim FirstName
…
Snippet from /app1/buffer__1.asp|
Previous Best: |
7.05 |
msec/page |
|
|
|
Response Time: |
6.08 |
msec/page |
|
|
|
Difference: |
-0.97 |
msec |
13.7% |
decrease |
|
Previous Best: |
7.05 |
msec/page |
|
|
|
Response Time: |
5.57 |
msec/page |
|
|
|
Difference: |
-1.48 |
msec |
21.0% |
decrease |
· Always enable buffering through server settings.
<% OPTION EXPLICIT
'-------------------------------------------------------------------------------
… 20 lines …
'-------------------------------------------------------------------------------
Dim FirstName
…
Snippet from /app2/comment_1.asp|
Benchmark: |
5.57 |
msec/page |
|
|
|
Response Time: |
5.58 |
msec/page |
|
|
|
Difference: |
0.01 |
msec |
0.1% |
increase |
·
When used in moderation, ASP
Comments have little or no impact on performance.
<%@ LANGUAGE=VBSCRIPT %>
<% OPTION EXPLICIT
Dim FirstName
…
Snippet from /app2/language1.asp|
Benchmark: |
5.57 |
msec/page |
|
|
|
Response Time: |
5.64 |
msec/page |
|
|
|
Difference: |
0.07 |
msec |
1.2% |
increase |
·
Set the server’s default language
configuration to match the language used on the site.
· Do not set the language declarative unless you are using the non-default language.
<%@ ENABLESESSIONSTATE = FALSE %>
<% OPTION EXPLICIT
Dim FirstName
…
Snippet from /app2/session_1.asp|
Benchmark: |
5.57 |
Msec/page |
|
|
|
Response Time: |
5.46 |
Msec/page |
|
|
|
Difference: |
-0.11 |
Msec |
2.0% |
decrease |
|
Benchmark: |
5.57 |
Msec/page |
|
|
|
Response Time: |
5.14 |
Msec/page |
|
|
|
Difference: |
-0.43 |
Msec |
7.7% |
decrease |
· Always disable Session state at the page or application level when not required.
|
Benchmark: |
5.57 |
msec/page |
|
|
|
Response Time: |
6.12 |
msec/page |
|
|
|
Difference: |
0.55 |
msec |
9.8% |
increase |
·
Always Use
Option Explicit in
VBScript.
…
CALL writeTable()
SUB writeTable()
Response.Write("<html>" & _
"<head>" & _
…
"<tr><td><b>EMail:</b></td><td>" & EMail & "</td></tr>" & _
"<tr><td><b>Birth Date:</b></td><td>" & BirthDate & "</td></tr>" & _
"</table>" & _
"</body>" & _
"</html>")
END SUB
Snippet from /app2/function1.asp|
Benchmark: |
5.57 |
msec/page |
|
|
|
Response Time: |
6.02 |
msec/page |
|
|
|
Difference: |
0.45 |
msec |
8.1% |
increase |
<% OPTION EXPLICIT
CALL writeTable()
SUB writeTable()
Dim FirstName
…
Dim BirthDate
FirstName = "John"
…
BirthDate = "1/1/1950"
Response.Write("<html>" & _
"<head>" & _
" <title>Response Test</title>" & _
"</head>" & _
"<body>" & _
"<h1>Response Test</h1>" & _
"<table>" & _
"<tr><td><b>First Name:</b></td><td>" & FirstName & "</td></tr>" & _
…
"<tr><td><b>Birth Date:</b></td><td>" & BirthDate & "</td></tr>" & _
"</table>" & _
"</body>" & _
"</html>")
END SUB
Snippet from /app2/function2.asp|
Benchmark: |
5.57 |
msec/page |
|
|
|
Response Time: |
5.22 |
msec/page |
|
|
|
Difference: |
0.35 |
msec |
6.3% |
decrease |
·
Encapsulate code into function
blocks when the code will be used more than once in a page.
·
Move variables declarations into
function scope when appropriate.
<% OPTION EXPLICIT
Dim FirstName
…
Dim BirthDate
FirstName = "John"
…
BirthDate = "1/1/1950"
%>
<!-- #include file="inc1.asp" -->
Snippet from /app2/include_1.asp|
Benchmark: |
5.57 |
msec/page |
|
|
|
Response Time: |
5.93 |
msec/page |
|
|
|
Difference: |
0.36 |
msec |
6.5% |
increase |
<% OPTION EXPLICIT
Dim FirstName
…
Dim BirthDate
FirstName = "John"
…
BirthDate = "1/1/1950"
CALL writeTable()
%>
<!-- #include file="inc2.asp" -->
Snippet from /app2/include_2.asp|
Benchmark: |
5.57 |
msec/page |
|
|
|
Response Time: |
6.08 |
msec/page |
|
|
|
Difference: |
0.51 |
msec |
9.2% |
increase |
·
Use Include files only when code can
be shared across pages.
<% OPTION EXPLICIT
On Error Resume Next
Dim FirstName
…
Snippet from /app2/error___1.asp|
Benchmark: |
5.57 |
msec/page |
|
|
|
Response Time: |
5.67 |
msec/page |
|
|
|
Difference: |
0.10 |
msec |
1.8% |
increase |
·
Only use error handling when a
condition can occur that is outside your ability to test or control.
<%@ TRANSACTION = REQUIRED %>
<% OPTION EXPLICIT
Dim FirstName
…
Snippet from /app2/transact1.asp|
Benchmark: |
5.57 |
msec/page |
|
|
|
Response Time: |
13.39 |
msec/page |
|
|
|
Difference: |
7.82 |
msec |
140.4% |
increase |
·
Only use transactions when two or
more operations MUST be performed as a unit.
<%@ LANGUAGE=VBSCRIPT %>
<%
On Error Resume Next
FirstName = "John"
…
BirthDate = "1/1/1950"
Response.Write("<html>")
Response.Write("<head>")
Response.Write(" <title>Response Test</title>")
Response.Write("</head>")
Response.Write("<body>")
Response.Write("<h1>Response Test</h1>")
Response.Write("<table>")
Response.Write("<tr><td><b>First Name:</b></td><td>" & FirstName & "</td></tr>")
…
Response.Write("<tr><td><b>Birth Date:</b></td><td>" & BirthDate & "</td></tr>")
Response.Write("</table>")
Response.Write("</body>")
Response.Write("</html>")
%>
Snippet from /app2/final___1.asp|
Benchmark: |
5.57 |
msec/page |
|
|
|
Response Time: |
8.85 |
msec/page |
|
|
|
Difference: |
3.28 |
msec |
58.9% |
increase |
· Avoid excessive use of Inline ASP.
· Always concatenate consecutive Response.Write statements into a single statement.
· Never use wrapper functions around the Response.Write statement to append CRLFs.
· If you must format your HTML output, append CRLFs directly within the Response.Write statements.
· Always enable buffering through server settings.
·
When used in moderation, ASP
Comments have little or no impact on performance.
·
Set the server’s default language
configuration to match the language used on the site.
· Do not set the language declarative unless you are using the non-default language.
·
Always Use
Option Explicit in
VBSCript.
· Always disable Session state at the page or application level when not required.
·
Use Include files only when code can
be shared across pages.
·
Encapsulate code into function
blocks when the code will be used more than once in a page.
·
Move variables into function scope
when appropriate.
·
Only use error handling when a
condition can occur that is out of your ability to test or control.
·
Only use transactions when two or
more operations MUST be performed as a unit.
·
Avoid redundancy – don’t set
properties that are already set by default.
·
Limit the number of function calls.
·
Narrow the scope of your code.
LINK
TO ZIP FILE