How fast ajax
The design and construction of a computer program can involve thousands of decisions, each representing a trade-off. In difficult decisions, each alternative has significant positive and negative consequences. In trading off, we hope to obtain a near optimal good while minimizing the bad. Perhaps the ultimate trade-off is:. There must be a trade-off. In computer programs, we see time versus memory trade-offs in the selection of algorithms. We also see expediency or time to market traded against code quality.
Such trades can have a large impact on the effectiveness of incremental development. Every time we touch the code, we are trading off the potential of improving the code against the possibility of injecting a bug.
When we look at the performance of programs, we must consider all of these trade-offs. When looking at optimization, we want to reduce the overall cost of the program. Typically, this cost is the perceived execution time of the program, although we could optimize on other factors. We then should focus on the parts of the program that contribute most significantly to its cost.
There is little benefit from optimizing components that do not contribute significantly to the cost. The analysis of applications is closely related to the analysis of algorithms. When looking at execution time, the place where programs spend most of their time is in loops.
The return on optimization of code that is executed only once is negligible. The benefits of optimizing inner loops can be significant. For example, if the cost of a loop is linear with respect to the number of iterations, then we can say it is O n , and we can graph its performance as shown in Figure The execution time of each iteration is reflected in the slope of the line: the greater the cost, the steeper the slope. The fixed overhead of the loop determines the elevation of its starting point.
There is usually little benefit in reducing the fixed overhead. Sometimes there is a benefit in increasing the fixed overhead if the cost of each increment can be reduced. That can be a good trade-off. In addition to the plot of execution time, there are three lines—the Axes of Error—that our line must not intersect see Figure The first is the Inefficiency line.
This can also make people irritable. The second is the Frustration line. When this line is crossed, the user is aware that he is being forced to wait. This invites him to think about other things, such as the desirability of competing web applications. The third is the Failure line. This is when the user refreshes or closes the browser because the application appears to have crashed, or the browser itself produces a dialog suggesting that the application has failed and that the user should take action.
There are three ways to avoid intersecting the Axes of Error: reduce the cost of each iteration, reduce the number of iterations, or redesign the application. When loops become nested, your options are reduced. If the cost of the loop is O n log n , O n 2 , or worse, reducing the time per iteration is not effective see Figure The only effective options are to reduce n or to replace the algorithm.
Fiddling with the cost per iteration will be effective only when n is very small. Programs must be designed to be correct. As with GET versus POST , there are other factors in selecting a data format, but do be aware that plain text will normally transmit the fewest bits of data, whereas XML can be verbose.
On a more advanced level, you can compress the data on the server before returning it. Modern browsers handle GZipped data well, although there is a trade-off in the extra processing required to GZip and unzip the data on either end. Transmitting GZipped data is a bit more advanced a concept, but one that should be on your radar. If you have the ability to manipulate how your server runs, the performance of your Ajax requests can be improved by applying the same techniques used to improve the performance of any server request:.
Doing so places resources geographically closer to users, thereby cutting the network transfer speeds. If the server-side Ajax resource is hosted on a CDN, users will benefit from that, too. Reducing the number of requests performed, and the amount of data included in both sides of the transaction, will have a dramatic impact on your Ajax performance.
Making server changes will help, too, but may be beyond your control. Completely within your control are some code-based considerations you should be aware of. The most complicated script in that chapter can perform two types of Ajax requests:. With this particular case, Ajax requests are only necessary if the auction is open, so code first checks for that condition. That check alone rules out all potential and unnecessary Ajax requests that would be made should any user view an expired auction.
Second, many users will view an auction but not bid on it, so the XMLHttpRequest object for performing bids is not initialized until the user submits a bid for the first time. Both are destroyed as soon as the auction closes.
Beyond just the creation and destruction of the XMLHttpRequest objects, you should take a close look at the other JavaScript code in a page: If it performs poorly, it will undoubtedly impact the Ajax performance, and generally degrade the user experience. But Ajax done poorly can be worse than not using Ajax at all. To minimize the potential for problems, and to create a more reliable user experience, incorporate the concrete suggestions outlined in this article into your next Ajax project.
I would like to receive exclusive offers and hear about products from Peachpit and its family of brands. I can unsubscribe at any time. Pearson Education, Inc. This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies. To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:.
For inquiries and questions, we collect the inquiry or question, together with name, contact details email address, phone number and mailing address and any other additional information voluntarily submitted to us through a Contact Us form or an email.
We use this information to address the inquiry and respond to the question. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes. Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites.
Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites; develop new products and services; conduct educational research; and for other purposes specified in the survey. Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing.
Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law. If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email ask peachpit. On rare occasions it is necessary to send out a strictly service related announcement.
For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature. We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.
Pearson automatically collects log data to help ensure the delivery, availability and security of this site. The index. It now will look something like this:. The file called can contain whatever content you want to appear on the page. Then just to stop it changing suddenly, we hide everything and then fade it in. The contents of the new PHP file is short and sweet, it takes the page number that jQuery has sent it and looks to see if the corresponding HTML file exists.
If it does it gets all the content and returns it to the AJAX function which we showed a moment ago that we insert that content into the main DIV. The site now calls in the right corresponding HTML file each time the user clicks on a navigation link. It swaps out the content without making the page reload. You can view the jQuery demo here, the PHP demo here, or download the source and take a closer look. Have you used AJAX for loading content?
0コメント