Cache Your Object
by Geethalakshmi[ Edit ] 2010-09-16 21:03:51
Cache Your Object
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:
<script type="text/javascript">
var theimages=document.images
for (i=0;i<theimages.length;i++)
theimages[i].src="blank.gif"
</script>
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.