This article mainly introduces five major techniques for speeding up ASP. Interested friends can refer to it.
One of the tips: Improve the efficiency of using Request collections
Accessing an ASP collection to extract a value is a time-consuming and computing resource consuming process. Because this operation contains a series of searches for related sets, this is much slower than accessing a local variable. Therefore, if you intend to use a value from the Request collection multiple times in the page, you should consider storing it as a local variable. For example, write the code into the following form to speed up the script engine processing:
- strTitle=Request.Form(Title)
- strFirstName=Request.Form(FirstName)
- strLastName=Request.Form(LastName)
- IfLen(strTitle)ThstrTitle=strTitle&
- IfstrFirstName=ThstrFullName=strTitle&&strLastName
- ElseifLen(strFirstName)=1Then
- strFullName=strTitle&strFirstName&.&strLastName
- Else
- strFullName=strTitle&strFirstName&&strLastName
- EndIf
Tips 2: Direct access to the appropriate collection
If not, don't use strPage=Request(page) to get parameters, because this will search all collections QueryString, Form, Cookies, ClientCertificate, ServerVarible in order until the first matching value name is found . This is less efficient than directly accessing the appropriate set and is unsafe unless it is absolutely guaranteed that this value will not appear in another set.
For example, it may be desirable to search for the WEB server name that satisfies the client's request, which is accomplished by looking for SERVER_NAME in the Request.ServerVarables collection that appears in each query. However, if other sets also contain values named SERVER_NAME (key names are case-insensitive), when using Request(server_Name), an incorrect result will be obtained. In short, the appropriate collection should be accessed directly as much as possible.
Tips 3: Use the Response.IsClientConnected property before time-consuming operation
Using Response.IsClientConnected is a useful way to observe whether the user is still connected to the server and is loading the web page created by the ASP. If the user disconnects or stops downloading, we no longer need to waste the server's resources to create web pages, because the buffer content will be discarded by IIS. So, for web pages that require a lot of time to calculate or use more resources, it is worth checking at each stage whether the visitor is offline:
- ...Codetocreate first part of the page
- IfResponse.IsClientConnectedThen
- Response.Flush
- Else
- Response.End
- EndIf
- ...Codetocreatenextpartofpage
Tips 4: Optimize ADO operations in ASP
Generally speaking, data constitutes the actual content of the WEB site. Therefore, it is very useful to optimize ADO operations to speed up ASP code execution:
a. Select only the columns you want: When opening the ADO record set, the table name (i.e. SELECT *) should not be automatically used unless all columns are required. Using a separate column means that the amount of data sent to or fetched from the server will be reduced. Even if you need to use all columns, naming each column individually will achieve the best performance, because the server no longer has to interpret the names of those columns.
b. Use stored procedures as much as possible. Stored procedures are pre-compiled programs that contain a ready execution plan, so they execute faster than SQL statements.
c. Use appropriate cursor and lock modes. If all the work is done is to read data from the record set and display it on the screen, then the default forward-only, read-only record set is used. The less work ADO uses to maintain the details of records and locking, the higher the performance of execution.
d. Use object variables. A sure way to improve performance when traversing a record set is to use object variables to point to members in the set. For example:
- WhileNotRsGc.EOF
- Response.Write project name: &RsGc(GcMC)&(Project code: &RsGc(GcCode)&)
- RsGc.MoveNext
- Wend
You can use rewritten as follows to speed up execution:
- setGcMc=RsGc(GcMc)
- setGcCode=RsGc(GcCode)
- WhileNotrsGc.EOFResponse.Write Project name: &GcMc&(Project code: &GcCode&)
- RsGc.MoveNext
- Wend
The new code creates references to object variables, so object variables can be used instead of actual variables, which means the script engine works less because the number of indexes in the set becomes less.
Tips 5: Don't mix script engines
We know that you can use both VBScript and JScript in ASP pages. However, it is not advisable to use both JScript and VBScript on the same page. Because the server has to instantiate and try to cache two (rather than one) script engines, this adds to the system burden to some extent. Therefore, from a performance perspective, multiple scripting engines should not be mixed in the same page.
The above are the five major techniques for speeding up ASP. By learning these five techniques, you can speed up the opening of the website.