Tanzim Saqib on .NET discovery

January 26, 2008

Appearing on Microsoft Volta team blog

Filed under: AJAX, Volta — Tanzim Saqib @ 9:58 pm

Microsoft Volta team blogged about me and one of my articles: http://labs.live.com/volta/blog/Volta+How+To+Flickr+Widget.aspx

VoltaTeamBlog

[New Article] ASP.NET AJAX Best Practices

Filed under: AJAX, JavaScript, Performance — Tanzim Saqib @ 7:22 pm

While we develop AJAX applications, we often carelessly ignore giving up bad practices, which cause effects which are not so significantly visible when the site is not so large in volume. But, it’s often severe performance issue when it is the case for sites that make heavy use of AJAX technologies such as Pageflakes, NetVibes etc.

There are so many AJAX widgets in one page that little memory leak issues combined may even result the site crash into very nasty “Operation aborted”. There are a lot of WebService calls, lot of iterations among collection so that inefficient coding in a whole lead to make site very heavy, browser eats up a lot of memory, requires very costly CPU cycles, and ultimately causes unsatisfactory user experience. In this article many of such issues are demonstrated in the context of ASP.NET AJAX: http://www.codeproject.com/KB/ajax/AspNetAjaxBestPractices.aspx

January 15, 2008

ASP.NET AJAX Best Practices: Avoid String concatenation, use Array instead

Filed under: AJAX, JavaScript, Performance — Tanzim Saqib @ 7:52 pm

Don’t you think the following block of code has written keeping every possible good practice in mind? Any option for performance improvement?

function pageLoad()
{
    var stringArray = new Array();

    // Suppose there’re a lot of strings in the array like:
    stringArray.push(‘<div>’);
    stringArray.push(’some content’);
    stringArray.push(‘</div>’);

    // … code edited to save space

    var veryLongHtml = $get(‘divContent’).innerHTML;
    var count = stringArray.length;

    for(var i=0; i<count; ++i)
        veryLongHtml += stringArray[i];
}

Well, as you see the innerHTML of the div has been cached so that browser will not have to access the DOM every time while iterating through stringArray, thus costlier DOM methods are being avoided. But, inside the body of the loop the JavaScript interpreter has to perform the following operation:

veryLongHtml = veryLongHtml + stringArray[i];

And the veryLongHtml contains quite a large string which means in this operation the interpreter will have to retrieve the large string and then concatenate with the stringArray elements in every iteration. One very short yet efficient solution to this problem is using join method of the array like the following, instead of looping through the array:

veryLongHtml = stringArray.join(); 

However, this is very efficient than the one we were doing, since it joins the array with smaller strings which requires less memory.

January 12, 2008

ASP.NET AJAX Best Practices: Introduce function delegates

Filed under: AJAX, JavaScript, Performance — Tanzim Saqib @ 7:33 pm

Take a look at the following loop. This loop calls a function in each iteration and the function does some stuffs. Can you think of any performance improvement idea?

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

Well, for sufficiently large array, function delegates may result in significant performance improvement to the loop.

var delegate = processElement;

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

The reason behind performance improvement is, JavaScript interpreter will use the function as local variable and will not lookup in its scope chain for the function body in each iteration.

January 11, 2008

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

Filed under: AJAX, JavaScript, Performance — Tanzim Saqib @ 7:28 pm

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]);   
Newer Posts »

Powered by WordPress