Faster Pages by Making Less Requests with Diet HTTP August 23rd, 2009

We’ve been cooking up a new project called Diet HTTP, which are designed for use with PHP with support for Smarty. The idea here is to cut down on HTTP requests for .css and .js by serving the files in bundles instead of individually. This will improve your page load time by:

  • Cutting down on HTTP requests, which are limited to 2 per domain. If you know your front page will require 10 CSS files for the different components or widgets, then just serve it as one single request.
  • Lower total size of files due to increased compression dictionary. For instance, if a CSS property is used twice it will likely reference the dictionary second time. (Compare to making a tar.gz of all your cs/js files to just zipping them)
  • “Bundles” will stay cached on the client and each bundle is a unique ID. Areas that have different functionality on your site will require different bundles of stylesheets.
  • Minifies your Javascript and CSS (or uses minified files), but doesn’t alter any of your files. (Keeps a cache)

Basically bandwidth is better spent just transmitting the files you will likely need at a later time. A good example is with stylesheets. Most pages have a base stylesheet that gets applied to every page, let’s say that’s 15 KB. Let’s say the user navigates to another page, but they won’t have to download that base stylesheet again since it’s likely cached, but they will have to download the new stylesheet for things specific to that page. The idea is to create a “bundle” of stylesheets that should be loaded together, so they won’t have to load another until they navigate to some drastically different area.

The same concept is used for Javascript being served, although the impact is much greater. A common site will easily tip the scales at 150 KB of scripts before being optimized.

We’ll be making the Diet framework available under the LGPL license. While it will be part of our Twig framework, we think it’s important for it to be easy to separate.

Tip: Append to an array with + operator August 8th, 2009

You can use the addition and increment operators in PHP to concatenate arrays. For example, $c = $a + $b;

If you’ve used array_merge() to do this in the past, it’s important to note the difference between merging arrays and concatenating them. When you merge they keys are used to determine how the resulting array is formed. When you concatenate the values are added together.

Tip: Don’t close your PHP tags August 8th, 2009

Adding a closing ?> to your <?php tags is actually optional, and you should avoid using it for scripts that will only be included.

Read the rest of this entry »