15 kinds of ASP usage skills

Source: Internet
Author: User
Tags define array arrays definition execution file system object model variables

Tip 1: Use Global.asa for application-tier files
Tip 2: Remove the HTML annotation from the product source code
Tip 3: Multiple Response.Write declarations
Tip 4: Use the flag to sample objects
Tip 5: Use local (local) variables as much as possible
Tip 6: Avoid multidimensional arrays
Tip 7: Avoid public (global) variables
Tip 8: Use a text path
Tip 9: Use the "Dictionary" object
Tip 10: Take full advantage of the browser's fallback function
Tip 11: Avoid using server-side variables
Tip 12: Use "Option Explicit"
Tip 13: Copy the collected values to a local (local) variable
Tip 14: Use the Session object with caution
Tip 15: Do a performance test

* Tip 1: Use Global.asa for application-tier files
to load a file into an application-tier array in Global.asa instead of reading the file on the page by using an ASP file system object.
GLOBAL. ASA can be used to specify event scripts or to declare objects that have session or application scope. Instead of displaying directly to the user, it stores information and objects for application-level events. You can then point to an array of application layers that have data through the page. This means that every server-side request to enroll in a file is not read every page per user. You can run a stand-alone ASP file to refresh the contents of the application-tier array, and you can also consider using the Dictionary object (see Tip 9).
This technique can greatly speed up your system.
How will this trick be implemented?
If you are a scripting person, you must use a file system object to read the file into an array or a Dictionary object. It then reads the file in the Global.asa and places the array (with the contents of the file you read) or the Dictionary object in the application-level declaration. This allows the user to access information in an array or Dictionary object without having to submit the information each time through an ASP request.
But you might think, "What if the content in the cache needs to be updated?" I bet it's not going to work. Fact If the contents of the cache need to be updated, you can call an administrator-only accessible ASP file containing an executable application lock command script, update the cached information in an array or Dictionary object, and finally execute the application Lockdown command.

* Tip 2: Remove HTML annotations from product source code (IIS3.0 applicable)
Yes, it's hard to write. A joke, this does not mean you have to remove all the HTML comments (the script annotation is good), a large range of HTML text can be copied to the client in batches. In this way, your script will run faster on IIS3.0 (in IIS4.0, HTML annotations will no longer cause slower execution).

* Tip 3: Multiple Response.Write statements
If you are writing the output in a format in several places in your code, consider putting the results together and writing with a Response.Write statement. Then you'll look at your HTML code and the composition of the VBScript script. Instead of spreading HTML and VBScript scripts too far, try to write chunks of HTML and VBScript scripts.

* Tip 4: Use the flag example Object
If you need to point to objects that may not be needed, use the flag example instead of Server.CreateObject. The object is immediately generated with Server.CreateObject, and if you don't use it later, it's a waste of resources.

* Tip 5: Use local (local) variables as much as possible
(the new tips below will replace the earlier "define variables in a row") it contains some erroneous ideas:
Local variables are defined in subroutines and functions (that is, local-scoped variables) that are compiled into The word points to and puts it in a table. The points of these local variables can be completed by one compilation. The global variable is executed at run time. This means that local variables can be accessed several times faster than global variables. Also, multidimensional global variables are the slowest, and when a multidimensional global variable is used for the first time, the entire object model is searched for an object with the same name, before the new object is produced.
The following is a very common example:
Foo.bar.blah.baz = Foo.bar.blah.qaz (1)
If Foo.bar.blah.zaq = Foo.bar.blah.abc Then    The br> runtime produces the following result:
1 variable foo is defined as a global variable 2) variable bar is defined as a member of Foo (3) variable blah is defined as a member of the Foo.bar 4) variable Qaz is defined as a member of Foo.bar.blah 5) to invoke Foo.bar.blah.quaz ( 1) 6) Repeat 1 to 3. The system does not know if the call to Qaz changed the object model 1-3 steps must be rerun 7 to define Baz as Foo.bar.blah member, output value 8) Repeat 1-3, perform zaq9) repeat 1-3, perform ABC
as you can see, the efficiency is extremely low, The quickest way is to write the code in VBScript:
Set myobj = Foo.bar.blah ' Do the resolution of blah Oncemyobj.baz = Myobj.qaz (1) If myobj . Zaq = Myobj.abc then

* Tip 6: Avoid repeating the definition of an array
When we use dim, avoid redefining the array. Because you might want to use ReDim to redefine the size of the array. As for such an operation, if your machine memory is not very large, it is best to consider the worst in the beginning to set the length of the array or set the length of the best state, when it is necessary to use ReDim. Of course this doesn't mean adding memory, if you don't need it very much.
The following examples illustrate improper use of ReDim
In fact, at the beginning of the definition of MyArray (5), and later need to use ReDim to increase his size, such words may occupy some memory, but the speed will be much faster.

* Tip 7: Avoid public (global) variables
Do not use variables that are defined with public. If you are writing VBScript or accessing variables in ActiveX controls or Java applets, avoid common variables as much as possible. The Public keyword is usually designed for later use, and since public does not benefit you, it is best to use dim.
* Tip 8: Use absolute path
If possible, avoid using relative paths, and use absolute paths. Using a relative path will require IIS to return to the current server path, which means that the special requests for IIS cause low execution speed.
Note: Slow down, it's much more convenient to use a relative path to transplant anything.

* Tip 9: Use the "Dictionary" object
The Dictionary object provided in VBScript provides fast lookup and storage with any keyword data. The Dictionary object allows you to access the data in the array based on the keyword so that you can find the content that is not contiguous in memory faster (because you are specifying the keyword you are using rather than knowing where the object is stored in the array). If you're looking for non-linear keyword data, it's much quicker to use the Dictionary object.
However, if the keyword data is contiguous in memory, the array will be faster to find and store the data. It is also necessary to note that indexing in dictionary is slower than in arrays. You should choose the data structure that works best for you.
* Tip 10: Take full advantage of the browser's fallback function
If you are using a smart browser, then he will help you do a lot of rollback work, as long as it is useful to use more. Then, perform a fallback through your script, and you can go back to the front when anything goes wrong and access the database from behind. But keep in mind that when you access a database that exists on the server, you perform a database operation once. If the form you want to return has a lot of variables, it's a bit out of the way. If you do know that you need to execute a lot of code on the client, move the code to the client to speed up execution. When you run on the client, the processor is yours, and the server has to use his own processing power to handle the requests he has received.
There's also a good idea that if you use a form that uses a lot of server-side scripting and has a lot of input, it's best to put the trigger code in the client script engine (such as Vbscript,javascript). Ignoring these, the server code runs fast, because the code that is not very necessary is not sent to the server side for execution. Of course, this is only for those relatively small code to apply, as for the big, it is not appropriate.

* Tip 11: Avoid using server-side variables
Accessing data through server-side variables requires the web to make a request to the server and then collect all the server-side variables, not just the variable you requested. It's like you're looking for something special from a box in a moldy attic. When you are looking for that thing, you first need to find the box from the attic. When you request a variable, the server is the same, triggering execution when it encounters the variable you requested, and then requesting the variable that does not cause the execution point.

* Tip 12: Use "Option Explicit"
Write in the ASP file. Unlike C, VB allows you to use this variable before you can force a variable to be defined. The Option Explicit opens to help identify undefined variables, and error messages appear using undefined variables. It is also possible to make the declared local variables illegal. A local variable that is not declared is as slow as a global variable (one times slower than a defined local variable). Turning on Option Explicit will naturally help you remove these bugs from your code.
* Tip 13: Copy the collected values to local (local) variables
If there are some values that you want to use repeatedly, copy the values to the client in the form of local variables. Each time you use these values, it saves you from going to the pile of values, which speeds up the script.

* Tip 14: Use the Session object with caution
Use the session object to store some user-specific information. When a user jumps between different pages of the application, the variables stored in the session are not lost, and instead, the variables are retained throughout the user's process. When a page is requested by a user who does not have a session, the Web server automatically establishes a sessions object. When the session time is limited to or is interrupted, the server undoes the sessions object. To avoid this, you can turn the session property off. However, the session attribute in iis3.0 in each application cannot be closed. Turning the session off throughout the server will be faster, but it will lose a lot of functionality. It is best to use the session object cautiously when needed
When you use the session object throughout the application, be careful to use it quickly, otherwise the session object will be reset. In Iis4.0, the session state in each application base can be activated, or it can be canceled in the specified.asp file.

* Tip 15: Do Performance testing
This is nothing to turn, a good knife is to wear a lot of grinding, good procedures are the same, more use, you know where the fault.
Tuning is not technology, it's art!
Optimizing your program is not a trivial matter, you need to start from a little bit, start from every detail, and develop good programming habits. I study the language, like to have a solemn style.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.