AJAX based web apps and performance
AJAX libraries have simplified developer's life by providing clean
& easy-to-use API. Their usage is so simple that we developers over
use it, without realizing the performance impacts.
Most articles on the web explain when and how to use AJAX, taking simple scenarios into consideration. But in the case of complex business applications, AJAX can be a performance killer, if used in the wrong way. This article explains few scenarios in which AJAX can be an overkill for your web apps.
The motto is to help fellow developers take better design decisions at an early stage, rather than fine tuning later. Below are real time problems, faced in a large scale AJAX based web app, which should be given a serious thought!
(1) AJAX based navigation:
"Keep DOM manipulations to the minimum" is what every JavaScript library says, but over use of AJAX based navigation defeats this purpose and you may rethink the design, unless your client specifically wants it.
If you are wondering what AJAX based navigation means, check jqGrid demos site. There are no post backs at all even while navigation. Content pages are fetched via AJAX and injected into a parent page, with a huge DOM manipulation.
If the page is small with very less number of DOM elements, AJAX navigation is fine. As the number of elements increase in the page, injecting the page takes longer time and beyond a point, causes 'stop running script' error. Typically, business applications contain hundreds of controls in a page, causing severe performance bottlenecks.
If you see Facebook, the Home, Profile, Account etc links on the top do a full post back and fetch the page, while any other operation is an AJAX call, which is a cooler approach.
Bottom line: AJAX navigation has performance problems when pages are huge. But it can solve problems like maintaining state by storing data in DOM elements, reducing session variables and reducing load on the server. So weigh these choices before taking a call.
You may find the total number of elements in the page using the jQuery code, $('*').length; So be cautious of this count while injecting a page. In a complex page like Yahoo.com, there are about 780 elements (each html tag corresponds to one element). Make sure your page is having not more than 1000 DOM elements. If the count is running into thousands, then split your pages.
(2) Client side templating:
If you liked asp.net repeater and are looking for client side templating, hold on! There is a difference between asp.net repeater and client side templates.
In the case of a repeater, processing takes place on the server and not much load is on the browser. However, in the case of templates, processing as well as injection is on the client. Imagine templating 100 rows, with each row containing 30 elements. You end up having 3000 elements which is alarming!
I can give you the example of twitter.com or facebook.com. Both have a 'more' sort of button at the bottom, which fetch more records. What happens if you want to see the posts of last ten days? You end up with thousands of DOM elements and your browser slows down.
Bottom line: In terms of performance, what is apparent to the developer is only the time taken to process the template. But what is hidden is, the time taken for clearing events, handling memory leaks, cleaning missing tags and injecting the template. All this happens in jQuery's .html() method.
So, if you want to template huge data, make sure you are implementing pagination. Again, as in the above case, $('*').length is the key.
(3) Tabs:
Thanks to this fancy UI technique, which gives a wizard sort of appearance to the content. If you are looking only at the fundo part of it, you are getting into problems! The scenario gets worst when you have AJAX tabs which fetch huge pages.
Let's say each page has ~700 elements. So if you have 5 tabs, you are having ~3500 elements. Imagine having blur, click, live events for so many elements. It's a complete mess! Also, you will be running into context related issues, since 2 tabs can have 2 different controls with the same ID. When you are on an active tab, the content of the rest of the tabs is only hidden, but not removed. So your app's performance is bad yet again.
Bottom line: If you want to use tabs with optimal performance, make sure you are clearing the mark up of the tabs which are not active. At any point of time, make sure your $('*').length is always less than 1000, for better performance .
Hope the article helps AJAX developers in taking better design decisions in their projects. More such scenarios will be covered in forth coming articles. Happy programming :)
Krishna Chaitanya T
Senior Systems Engineer - Future Web Research Lab, SETLabs
KrishnaChaitanya_T@infosys.com
www.novogeek.com


