Friday, January 11, 2008

ASP.NET AJAX Best Practices: Introduce DOM elements and function caching

We have seen DOM caching before and function delegation is also a kind of function caching. Take a look at the following snippet:
for(var i=0; i<count; ++i)
$get('divContent').appendChild(elements[i]);

As you can figure out the code is going to be something like:

var divContent = $get('divContent');

for(var i=0; i<count; ++i)
divContent.appendChild(elements[i]);

That is fine, but you can also cache browser function like appendChild. So, the ultimate optimization will be like the following:

var divContentAppendChild = $get('divContent').appendChild;

for(var i=0; i<count; ++i)
divContentAppendChild(elements[i]);

No comments: