PART @: This quick tips will teach you how you can get the best out of your ASP application. Part one will be discussing about some basic tips and part two will be covering in a lot more detail.
PART @: This quick tips will teach you how you can get the best out of your ASP application. Part one will be discussing about some basic tips and part two will be covering in a lot more detail.
On the Part 1 of this article, I’ve talked about 5 tips to speed up your ASP page. The second part of this article will be much more interesting as I’ll be talking yet another 5 more great tips for your asp needs!
When a browser asks for a page from a server, it is called a request. The ASP Request object is used to get information from the user. This code is what normally programmers do:
<%
Dim name
name = Request("name")
%>
The request object has FIVE COLLECTION plus a default collection. The default collection is what you are seeing above. The other collections are:
Every ASP programmer is familiar with these collections. They are pretty much the most crucial tools available in the Request object. They allow you to gain access to the client’s request information.
When you do a call to the default Request collection, each of the sub collections are searched in that following order.
The process stops when the first match is found. If you’re passing a lot of information between pages, using the default collection to get inputs can result in slowdowns, especially if you’re looking for servervariables that way. What I usually do is use the default collection if I am looking for form or querystring variables only. That provides a bonus because then the application works with both GET and POST request.
This is a common mistake that programmers make. Consider this code which illustrates what NOT to do:
<%
If Request.Form("name") <> "" Then
SQL = "INSERT INTO table1 (name) VALUES ('" & Request.Form("name") & "')"
Response.Write "Your name is : " & Request.Form("name")
End If
%>
Wherever ASP objects are used, it is better to store session or querystring objects in a variable at the start of the page and subsequently use the variable. What’s happened above is the Request.Form collection keeps getting called over and over again for the “name” input, and yet we know that the input’s value hasn’t changed. The Engine needs to read the value of from the Querystring collection each time which increases overhead. This translates to cycles wasted and a slowdown in the ASP application. A better way to write that code that would run your ASP page much faster would be like this:
<%
Dim name
name = Request.Form("name")
If name <> "" Then
SQL = "INSERT INTO table1 (name) VALUES ('" & Request.Form("name") & "')"
Response.Write "Your name is : " & Request.Form("name")
End If
%>
Now the collection has only been searched for that input one time. Effectively using this technique throughout your ASP pages will definitely speed up the page.
Database work takes cycles. Consider bad code like this which opens a database more than one time:
<%
Dim conn, rs, id
Set conn = Server.CreateObject("ADODB.Connection")
conn.open "connstring"
Set rs = Server.CreateObject("ADODB.recordset")
rs.open "SELECT id FROM table1 ORDER BY id DESC", conn
id = rs.fields(0).value
rs.close
Set rs = nothing
'Close Database Connection!
conn.close
Set conn = nothing
'Re-Open The Database Connection!
Set conn = Server.CreateObject("ADODB.Connection")
conn.open "connstring"
conn.execute "INSERT INTO table1 (id) VALUES(" & id + 1 & ")"
'Close Database Connection!
conn.close
Set conn = nothing
%>
What you see above is code which gets a value out of a database and then creates a new row in the same database. This code makes the mistake of closing the database between sql executions. That code will be slower than it could be by rewriting it properly like this:
<%
Dim conn, rs, id
'Open Database Connection
Set conn = Server.CreateObject("ADODB.Connection")
conn.open "connstring"
Set rs = Server.CreateObject("ADODB.recordset")
rs.open "SELECT id FROM table1 ORDER BY id DESC", conn
id = rs.fields(0).value
rs.close
Set rs = nothing
conn.execute "INSERT INTO table1 (id) VALUES(" & id + 1 & ")"
'Close Database Connection!
conn.close
Set conn = nothing
%>
The difference is, the database connection is only opened one time above, saving time. When used comprehensively on every page of a website, these guidelines can make your applications run as fast as possible in the ASP environment.
When working with an ADO Recordset object, there are a number of ways to grab the value of a particular column. The most common way is to use the column’s name. For example, if we had a Recordset object named objRS that had three columns, Name, Age, and Salary, we could display these contents of these three columns for the current row in the Recordset using the following code:
<%
Employees Name: <%=objRS("Name")%><br>
Employees Age: <%=objRS("Age")%><br>
Employees Salary:<%=objRS("Salary")%><br>
%>
These values can also be accessed ordinally. Each column is given an ordinal index starting at zero. Hence, these three columns could also be displayed with the slightly modified code:
<% Employees Name: <%=objRS(0)%><br> Employees Age: <%=objRS(1)%><br> Employees Salary: <%=(objRS(2)%><br> %>
Now, you may say that using the ordinal notation decreases code readability, and I would agree.
There is a disadvantage of using this method but it can easily be solved. Consider this situation, if more than one person is working on a site, it could get you in trouble. Someone else adding one field to the SQL string NOT at the end will throw off all your code. The solution is to use a constant.
You could use constant to refer to your recordset elements. Although this has a very slight performance penalty in terms of script size and initial interpreter processing, it represents a nice trade-off between code readability and efficiency, example:
<%
strSQL = "SELECT Name, Age, Salary from People ORDER BY id"
Const NAME = 0
Const AGE = 1
Const SALARY = 2
objRS.Open strSQL, conn
Response.Write ("Employees Name: " & objRS(NAME) & "<br>")
Response.Write ("Employees Age: " & objRS(AGE) & "<br>")
Response.Write ("Employees Salary: " & objRS(SALARY) & "<br>")
%>
I think that’s a nice compromise between performance and maintainability/readability…
Use .htm files when possible as they never block and get maximum caching benefits. ASP files are generally blocked. So if the file contains pure HTML, give it an .htm extension. If you can avoid tables, that’s good news but we can’t always do that, so try to avoid nested complex tables. This will boost the load performance. The solution to this is by using CSS (Cascading Style Sheet) to position your HTML elements. As I said before, HTML file is executed 3 to 4 times faster than complex ASP script.
Now that I’ve made you sufficiently worried and excited at the same time about your asp applications, I must hasten to tell you that this is only the tip of the iceberg. Best coding practices don’t come easy. It comes with a passion for self-improvement. Enjoy coding…
Source: ASP101 4GuysFromRolla
Hey, great post, very well written. You should post more about this.
Cool post, just subscribed.