Tip 1: Cache your objects!
One of the best kept secrets to boosting script performance is to cache your objects. Often times, your script will repeatedly access a certain object, as in the following demonstration:
The term "cache your object" means storing a repeatedly access object inside a user defined variable, and using that variable instead in subsequent references to the object. The performance improvement can be significant. Here's a modified version of the initial script using object caching:
Not only is the number of times document.images is referenced cut in half with the above, but for each time it is referenced, the browser doesn't have to go through document.images first, but goes straight to its containing array.
Remember to use object caching when calling highly nested DHTML objects, like document.all.myobject, or document.layers.firstlayer etc.
Tip 2: Cache your scripts!
You've "cashed in" your objects...another way to enhance script performance is the cache the entire script, by including it in a .js file. The technique causes the browser to load the script in question only once, and recall it from cache should the page be reloaded or revisited.
Use script caching when a script is extremely large, or embedded across multiple pages.
- Tutorial Introduction
- Understand the cost of your objects