AJAX - The Complete Reference

(avery) #1

PART II


Chapter 6: Networking Considerations 261


slow network connection. Falling back to an old style of interaction may help if the problem
is with the XHR or there is a different server that could be called. Simply informing the user
that they are not able to use the application would certainly be better than silently failing.
We’ll spend significant time in Chapter 9 pondering how to build such applications, but for
now your exposure to what could happen is complete. However, before wrapping up the
chapter, let’s assume that connections are working fine, they just aren’t fast enough. It’s not
as dire as timeout but a slow application can just as certainly send users away in disgust.

Improving Ajax Performance


The promise of Ajax from the end-user’s perspective is richness and speed. While partial
page updates can visually change a user’s perception of the speediness of a Web
application, it is in fact quite possible to build a slow Ajax application. A few too many trips
over the network, even for small payloads, can just as easily induce delays and cause user
frustration as a few big downloads. To help mitigate this potential problem, let’s pause here
to take a quick tour of some simple performance improving techniques that can be
employed in a Web application starting with the golden rule of Web performance:

Web Performance Golden Rule: Send little, less often.

In a more wordy form you might say:

To improve Web site performance, you should aim to send as little data as required and not ask
for more data or re-request data unless you need to.

No matter how you say it, the performance golden rule directly promotes two ideas:
compression and caching. We’ll start first with compression and look at two forms of
compression that can be useful in Ajax applications.

HTTP Compression


HTTP compression is a long-established Web standard that has been around since 4.x
generation browsers but is still being discovered by Web developers. The basic idea of
HTTP compression is that a standard gzip or deflate compression method is applied to the
text payload of an HTTP response such as XHTML, CSS, JavaScript, and XML, significantly
reducing its size (often by as much as 70 percent) before transmission.
User agents may send an Accept-Encoding header indicating the type of content encoding
that the browser can accept beyond the standard plain text response—in this case gzip- and
deflate-compressed content. You see such headers being sent in two network traces here:
Free download pdf