Skills | performance
Tip 19: Use the browser's validation capabilities
Today's browsers provide support for advanced features such as XML, DHTML, Java applets, and remote Data services. Use these features whenever possible. All of these technologies can perform client-side validation and data caching, eliminating round-trip to the WEB server. If you are running a smart browser, the browser can do some validation for you (for example, check the credit card checksum for validity before performing the POST). Use this functionality whenever possible. Reducing the round-trip between the client and server reduces the load on the WEB server and reduces network traffic (although the first page sent to the browser may be larger) and any back-end resources that the server accesses. In addition, users do not have to read new pages as often as they do, so the user feels better. Doing so does not mean that you can not perform server-side validation-you should also always perform server-side validation. This prevents the client from generating the wrong data for some reason, such as a hacker, or the browser does not run a client-side validation routine.
People have done a lot of work to develop "browser-independent" HTML. Because of this concern, developers are reluctant to use popular browser features, but these features could have improved performance. For some truly high-performance sites, you must be concerned about the browser "access" problem, a good strategy is to optimize the page to adapt to the popular browser. Browser functionality can be easily detected in ASP using the browser feature component. Tools such as Microsoft FrontPage help you design code that is appropriate for browsers and for the specified HTML version. See when Better worse? Weighing the Technology trade-offs to understand further discussion.
Tip 20: Avoid using string concatenation in circular statements
Many people create a string in a loop statement, as follows:
s =? <table>? & VbCrLf
For each fld in Rs. Fields
s = S &? <th>? & FLD. Name &?</th>?
Next
While not Rs. Eof
s = S & vbCrLf &? <tr>?
For each fld in Rs. Fields
s = S &? <td>? & FLD. Value &?</td>?
Next
s = S &? </tr>?
Rs. MoveNext
Wend
s = S & vbCrLf & </table>? & VbCrLf
Response.Write S
There are some problems with this approach. The first problem is that it takes two of times to repeat the string, and more generally, the time it takes to run the loop statement is proportional to the square of the number of records multiplied by the number of fields. A simpler example would give a clearer picture of the problem.
s =??
For i = ASC (?) A?) To ASC (? Z?)
s = S & Chr (i)
Next
In the first iteration, did you get a string of characters? A?. In the second iteration, VBScript must reassign the string and two characters (? AB?) Copy to S. In the third iteration, it must reassign s again and copy three characters to S. In
NSub (26th time) iteration, it must be reassigned and
NCharacters are copied into S. The total is 1+2+3+...+
N, namely
N*(
N+1)/2 times copy.
In the above recordset example, if there are 100 records and 5 fields, the inner loop will perform 100*5 = 500 times, and all the time spent copying and reassigning is proportional to the 500*500 = 250,000. This is too many replication operations for a medium sized recordset.
In this case, the code can use Response.Write () or inline script (<% = fld. Value%>) instead of string concatenation to improve. This is faster if response buffering is enabled, because Response.Write only attaches the data to the end of the response buffer. Does not involve redistribution, so it is highly efficient.
You should consider using GetRows or GetString in specific cases where you convert an ADO recordset to an HTML table.
If you are concatenating strings in JScript, it is particularly recommended to use the + = operator, that is, to use S + + to use a string instead of S = S +? A string?.
Tip 21: Enable browser and proxy caching
By default, ASP prohibits caching in browsers and proxies. This is meaningful because, in essence, the ASP page is dynamic, with potential information changing over time. If the page does not require a refresh on each view, you should enable the browser and the proxy cache. This allows browsers and agents to use a "cached" copy of the page within a certain amount of time, and you can control the length of time. Caching can greatly reduce the load on the server and shorten the user's waiting time.
What kind of dynamic page can be used as a page to cache? Here are some examples:
- Weather forecast page, which updates the weather forecast every 5 minutes on this page.
- The homepage of a news entry or newsletter is updated two times a day.
- A list of mutual fund performance, in which basic statistics are updated every few hours.
Note that the number of accesses recorded on the WEB server is reduced when using a browser or proxy cache. If you want to accurately measure all page views or post postings, you do not want to use the browser and proxy caching.
The browser cache is controlled by the HTTP "expired" header, which is sent to the browser by the WEB server. ASP provides two simple mechanisms for sending this header. To set the page to expire after a few minutes, set the Response.Expires property. The following example tells the browser that the content expires in 10 minutes:
<% Response.Expires =%>
If you set the Response.Expires to negative or 0, the cache is disabled. Be sure to use a large negative number, such as-1000 (slightly more than a day), to avoid a mismatch between the server and the browser clock. The second property Response.ExpiresAbsolute will let you set the time when the content expires:
<% Response.ExpiresAbsolute = #May 31,2001 13:30:15#%>
You can not use
ResponseObject sets the expiration time, and the <META> tag is written into HTML, usually written in the <HEAD> section of the HTML file. Some browsers will follow this directive, but the proxy is not.
<meta http-equiv=? Expires? Value=? May 31,2001 13:30:15?>
Finally, you can use the Response.CacheControl property to indicate whether its contents allow the HTTP proxy to be cached. If this property is set to public, the agent can cache this content.
<% Response.CacheControl =? Public? %>
By default, this property is set to Private. Note that for pages that display specific data for a particular user, proxy caching should not be enabled because the proxy may provide users with pages that belong to other users.
Tip 22: Use Server.Transfer instead of Response.Redirect whenever possible
Response.Redirect let the browser request another page. This function is often used to redirect users to a login or error page. Because redirection forces a new page to be requested, the result is that the browser must travel two times to the Web server, and the Web server must handle one more request. IIS 5.0 introduces a new function Server.Transfer that transfers execution to another ASP page on the same server. This avoids the extra browser-web-the server, which improves overall system performance and shortens user response times. Check the "New direction" in redirect, which should be Server.Transfer and Server.Execute.
See also leveraging ASP in IIS 5.0 for a complete list of the new features of IIS 5.0 and ASP 3.0.
Tip 23: Use the back slash in the directory URL
A related trick is to make sure that the backslash (/) is used in the URL that points to the directory. If you omit the backslash, the browser makes a request to the server, just to tell the server that it is requesting the directory. The browser issues a second request, which appends the slash to the URL, after which the server can respond with the default document or directory list for that directory (if there is no default document and directory browsing enabled). Additional slashes will save the first and useless live returns. To make it easier for users to read, you can omit the back slash in the display name.
For example, write:
<a Href=?http://msdn.microsoft.com/workshop
/? Title=? MSDN Web
Workshop?>http://msdn.microsoft.com/workshop</a>
This also applies to URLs that point to the home page on the Web site: using the following: <a Href=?http://msdn.microsoft.com/?>, instead of using <a href=?http://msdn.microsoft.com?>.
Tip 24: Avoid using server variables
Accessing a server variable causes the WEB site to send a special request to the server and collect all the server variables, not just the one you requested. This is similar to looking up a file in a moldy attic, in a folder. When you want to find the file, you must go to the attic and find the folder before you can find the file. When you request a server variable, the same happens-the first time you request a server variable, the performance is affected. Subsequent requests for other server variables do not have an impact on performance.
Never access an unqualified
RequestObject (for example, Request ("Data")). For items that are not in Request.Cookies, Request.Form, Request.QueryString, or request.clientcertificate, an implicit invocation of the Request.ServerVariables. The Request.ServerVariables collection is much slower than the other collections.
Tip 25: Upgrade to the latest and most outstanding
System components are constant and we recommend that you upgrade them to the latest and best configurations. It is best to upgrade to Windows 2000 (therefore, you should also upgrade to IIS 5.0, ADO 2.5, MSXML 2.5, Internet Explorer 5.0, VBScript 5.1, and JScript 5.1). On multiprocessor computers, implementing IIS 5.0 and ADO 2.5 can significantly improve performance. Under Windows 2000, ASPs can scale well to four processors or more, while in IIS 4.0 the ASP cannot be extended beyond two processors. The more scripting code and ADO you use in your application, the more performance improves after you upgrade to Windows 2000.