Tips to Improve Your Webpage Load Times

Categories: news, seattle — Tags: , , — Posted by: Grant @ February 28, 2008 : 3:38 pm

It’s a good news day, as the Coffee.net servers have been upgraded! Previously operating on the equivalent of a tall decaf, we are now jittering at double-shot venti levels with dedicated web, database and firewall servers. Combined with our improved back-end and front-end coding, our site should be rockin’ fast now.

For those web developers who wonder about the effect of load times, studies show [akamai.com] that the average visitor will leave a retail website if it doesn’t load in under four seconds. That’s quite a harsh penalty in absolute terms, but in the context of the web three seconds is an eternity. The good news is that most web pages do meet the three second rule. The bad news is that four seconds is the absolute pain threshold and that web developers can always do better.

So, for the casual webmaster out there, here’s a few quick PHP and mysql tips from the development side of Coffee.net on how to help beat that four second (and hopefully one second) rule:

  1. In your scripts, try to either stay in PHP or stay out of PHP for as long as possible. Constantly jumping in and out of PHP is inefficient and will cause forking unless you use a module like FastCGI.

    Good: echo ‘[html][h1]‘.$header.’[/h1][/html]‘;

    Bad: [html][h1][?php echo $header; ?][/h1][/html]

  2. Use single quotes instead of double quotes in your echo statements. Double quotes are processed for variables while single quotes are not. It adds a little time to your coding, but leads to cleaner and faster code

    Good: echo ‘You are visitor’. $visitor;

    Bad: echo “You are visitor $visitor”;

  3. When hitting your database, try to combine queries if possible, rather than constantly going back. Your sql server does not take kindly to taking request after request in a row and performs much better when given a giant query over a multiple small ones.

    Good: $result = mysql_query(’SELECT `firstname` FROM tblPayroll WHERE `lastname` = \’clinton\’ OR `lastname` = \’obama’\ OR `lastname` = \’mccain\’ OR …’)

    Bad: foreach ($candidates as $foo) { $result = mysql_query(’SELECT `firstname` FROM tblPayroll WHERE `lastname` = \”.$foo.’\'); }

  4. Use mysql_fetch_row or mysql_fetch_assoc rather than mysql_result. The speed is quite superior in large datasets, as it fetches an index and then moves the key up, rather than n-searching for a row number.

    Good: while ($vars = mysql_fetch_assoc($result)) { echo $vars['firstname']; }

    Bad: for ($i = 0; $i < mysql_num_rows($result); $i++) { echo mysql_query($result, $i, ‘firstname’); }

  5. Set proper indexes on your sql tables. Indexes help your database determine where a row is located in your table, rather than iterating over every single row looking for that specific item. S you have column ‘lastname’ and it has the values ‘clinton’, ‘obama’, ‘mccain’, ‘paul’ and ‘romney’ in that order. If you want to lookup ‘paul’, your database will first match against ‘clinton’, then ‘obama’, then ‘mccain’ and finally find ‘paul’ at the 4th row. Not horrible, until your database grows to a thousands of rows and now you’ve got 4,000 rows to search before finding your guy. By indexing the column, your database will sort the names alphabetically and assign values to each rows, such as 1-clinton, 2-mccain, 3-obama, 4-paul, 5-romney. Now when it looks for ‘paul’, it simply jumps to row 4 directly and bam, you’re done.

These are just a few quick tips on coding that address some of the major bottlenecks that most coders will encounter. Obviously, there are other ways to seriously beef up your load speed like (duh) upgrading to a dedicated server, caching, pre-compiling your php scripts, using compression and using heap (memory) tables for read-only operations. But if you know about these, then you probably already know all the basic tips anyways :)

Before we go, we want to thank the guys at Wowrack.com (Ed, Budi) for helping with our server setup. Wowrack is based out of the Westin Building, smack in the center of Seattle. We originally hosted our site with a very reputable facility on the East coast, but it made a lot more sense once we were out of the development cycle to bring our servers closer to home. After all, it just doesn’t make a lot of sense for internet traffic to travel 3,000 miles to access a website about the city you live in, right?

Cheers to our new servers!

Related posts:

  1. Lunch with MSG150
  2. Interviewed by Voice of America

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Chef Seattle Blog

About

News about Chef Seattle and stories from the front lines of eating, web coding and Seattle.

Home | About Us | Seattle Restaurants | Food Articles | Blog | Friends | Charity | Advertising | Contact Us
Blogging platform by WordPress
30 queries. 0.237 seconds.