http://www.perlmonks.org?node_id=639248


in reply to Re: AJAX popup windows - an example
in thread AJAX popup windows - an example

Interesting.

Don't take what I'm about to write as disagreeing with you. It is more about the thought process that went into my choice to use CGI::Ajax for the example. Your points are strong, although possibly not complete: You've validly pointed out that clarity and 'non-intrusiveness' suffers in my example. What you missed is that it traded it for performance.

I deliberately used CGI::Ajax to reduce the amount of explict Javascript I used for the example. You see, I normally just add the actual Javascript generated by CGI::Ajax as another block of output text - without actually using the CGI::Ajax module. I used the actual CGI::Ajax module here to make a clearer example without the burden of a full fledged toolkit like JQuery.

Sound backwards?

Not really. You see, performance and size tend to be my primary goals because I frequently need these kinds of scripts to be used on web pages that both receive many hits per day and that are already overly heavy byte-wise.

So my goals of "runs fast, loads fast" compromised with the goals of "code clarity, full featured" to settle on CGI::Ajax for the example to get "Ajax clarity, acceptable performance".

Code clarity would have argued for using one of the major JS toolkits. Performance would have argued for eliminating CGI::Ajax entirely and not using a toolkit library.

As a data point to illustrate my point on performance, I modified my script to follow my normal practice of inlining the Ajax support Javascript as part of my Perl and benchmarked it for 1000 requests at a concurrency of 20 using the 'ab' Apache benchmark script (with a dry run in each case to allow the server to 'get up to speed'). I also made the minimal modifications required to make run under FastCGI as another comparision point. The tests were run using Apache2 on a hyperthreaded 3Ghz P4 machine running Fedora Core 6 Linux (with a second machine making the HTTP requests).

CGI ModPerl2 FastCGI Original Example 24/sec 397/sec 328/sec Inlined Ajax JS 69/sec 505/sec 446/sec

Why such a dramatic slowdown when using CGI::Ajax? Because it pulls in a lot of extra Perl code to generate that chunk of Javascript. CGI::Ajax is about 41K, and it pulls in Exporter (14.4K), Data::Dumper (38K), base (5.4K), overload (46K), vars (2.3K), warnings::register (1K), and Carp (8.8K). For a startling 156K of Perl to generate just under 7K of final Javascript using CGI::Ajax. While the ModPerl2 and FastCGI environments are not nearly as sensitive to the byte count, there is still a noticable runtime overhead to the dynamic code generation of CGI::Ajax.

Similiar issues arise using the off-the-shelf Ajax libraries. They add dozens to hundreds of Kbytes of stuff needing to be loaded by the web browser (with dramatic performance consequences resulting just from that fact). Additionally, they tend to run slowly above and beyond that (Digg is a classic example - they use an off-the-shelf JS Ajax library that causes my machine to bog completely down on their longer pages. It is quite annoying.)

So, in addition to your (excellent) suggestion of covering the use of off-the-shelf Ajax JS libraries, I need to cover when-and-why NOT to use them.