#SAScon blog – Take Two - Compressing content and code acceleration

24 May 2011 | by Chris Marsh

We’ll pick up where we left off last time by looking at site optimisation. In this blog, we’re talking about compressing content and code acceleration – we also have a few CSS and JavaScript tricks to share.
Compressing content

By enabling compression on your web server, you’ll speed up the load times of pages on your site by compressing, both ASCII and binary files on the server side, as requested. This will leave the user to decompress them on the other side. Options to enable this are available for both Apache and IIS (Windows), and are supported on all modern web browsers. If a user doesn’t support compression, the server will simply revert to responding with uncompressed content.

The current standard choice is: mod_deflate (compatible alternative available for IIS).
Straightforward gzip compression is less popular, with deflate now being the preferred option: mod_gzip (compatible alternative available for IIS).

CSS and JavaScript tricks

There are a few quick and easy ways that you can optimise the use of, and structure of, your CSS and JavaScript code to enable quicker page loads. Performing these simple optimisation techniques is a time-efficient way to squeeze that little bit extra out of your load times.

  • Make CSS and JavaScript external to your HTML − basically, don’t embed any JavaScript or CSS in your HTML code. Making this external will cause a handful of extra connections to be made on the first visit, but once that happens your browser will cache these files. This means the files and the code contained won’t need to be downloaded again for some time.
  • Minification − This is the practice of stripping out white space, comments and other unnecessary data from scripts in order to reduce the over-all size of the code. While it won’t make the code run any faster, this has the advantage of enabling the data to be downloaded more quickly. Minification also works really well when used with the compression techniques mentioned above. We’d recommend Yahoo!’s YUI Compressor to handle the minification of your code. http://developer.yahoo.com/yui/compressor/

OpCode caching (or code acceleration)

Compilation is the process of turning code written by people into a language that the computer understands. Typically, this only needs to happen once. During run-time, high-level programming languages, such as PHP, operate in the same way that other languages behave in compilation.

Scripts written in languages like PHP are compiled ‘on-the-fly’ by an interpreter, which translates them into lower-level code that the computer understands. This happens every single time the PHP script is executed. At this point, I’m sure you can see why this can be wasteful in a server environment!

Fortunately, there are various tools and frameworks available to help reduce the waste. They help by caching the compiled code, so that it’s not re-compiled when it’s not needed. These tools include (but are not limited to):

  • eAccelerator − for PHP, available from http://eaccelerator.net/
  • APC (Alternative PHP Cache) − available at http://pecl.php.net/package/APC
  • XCache − for PHP, available from http://xcache.lighttpd.net/.

The truth is that there’s always more you can be doing, but we hope you’ve found something in these blogs to help you along the way. Do feel free to give one of the team at Melbourne a shout if you have any questions.

 

Like what you've read?