30

Note: This is meant to be a community wiki post

To try and make the user experience the best possible, what can I do to make the loading of my HTML pages more efficient?

fatihyildizhan
  • 8,614
  • 7
  • 64
  • 88
onteria_
  • 68,181
  • 7
  • 71
  • 64
  • Maybe this belongs on Webmasters? – Jimmy Sawczuk May 15 '11 at 01:07
  • 2
    @Jimmy, good point, but I do see many many posts here asking for help optimizing web pages. A good solid community wiki answer that addresses the issue might stave off some of the questions. I wonder if there's a good way to symlink/hardlink questions together on multiple SE sites... – sarnold May 15 '11 at 01:14
  • @sarnold I'd prefer symlink if there is. Otherwise I have to keep content up to date in multiple places, which could get a bit tiring – onteria_ May 15 '11 at 01:35
  • 3
    I don't think this belongs on Webmasters SE. First, speed is an engineering goal, whether it be a backend engineer using server-side code to properly architect an application or a frontend developer writing clean, semantically correct HTML. Also, check out the [Webmasters FAQ](http://webmasters.stackexchange.com/faq), which explicitly states that questions of interest to programmers belong on SO. See this meta post for more details: [link questions between sites](http://meta.stackexchange.com/questions/91307/link-questions-between-sites/91308#91308) – jamesmortensen May 15 '11 at 02:23

6 Answers6

21

When dealing with performance of pages, there are a few important methods to keeping your page load times quick.

CSS Organization

Try to minimize inline CSS styles and keep commonly used CSS rules together in external stylesheets. This helps keep reusable styles for later, and the lack of style attributes makes your HTML page download faster.

Minification

Since your CSS and Javascript includes have to be downloaded from your server to the client, smaller is always better. Yahoo has a great tool called YUI Compressor which can be used to reduce the size of your CSS and JavaScript includes. Popular libraries such as JQuery will also have both minified and development versions of their libraries available. Just remember to keep a copy of the non-minified version for debugging purposes!

Image Compression

You may want to consider compressing your images. For JPG files, try setting around 80% compression, and seeing how the result looks. You can play around with the levels until you get a decent result. For PNG files, you may want to look at some of the PNG compression tools available.

CSS Sprites

An interesting tactic in saving HTTP requests is the usage of CSS Sprites. The basic theory is instead of downloading multiple images, you simply download one large image with all of your images contained within it. This means instead of making continuous requests for image files, the browser just needs to make a single request. The tutorial CSS Sprites: What They Are, Why They’re Cool, and How To Use Them has some good information on the process, including how to convert from an existing multi-image layout.

Resource Ordering

When it comes to ordering your CSS and Javascript, you want your CSS to come first. The reason is that the rendering thread has all the style information it needs to rendering the page. If the Javascript includes come first, the Javascript engine has to parse it all before continuing on to the next set of resources. This means the rendering thread can't completely show the page, since it doesn't have all the styles it needs. Here is an example:

<link rel="stylesheet" type="text/css" href="/css/global.css" />
<link rel="stylesheet" type="text/css" href="/css/forms.css" />
<script type="text/javascript" src="/js/formvalidation.js"></script>

Tracking / Affiliate Script Locations

Many sites utilize tracking and/or affiliate scripts. If there is an issue with the remote host, and the scripts are included in the <head> tag, the browser has to wait for the downloads to occur before moving along. While such things are nice to have, they shouldn't slow down the user experience. It is recommended to move such scripts towards the bottom of the page, just before the </body> tag:

<!-- HTML Here -->
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
</body>

Missing Assets

Missing CSS and javascript files means the browser has to needlessly communicate with the server to grab files that don't exist. Depending on where the server is and how many files are missing, that could add up to slower page loads.

onteria_
  • 68,181
  • 7
  • 71
  • 64
  • 2
    Why would a missing asset cause a delay? Seems like a `404` should be even faster than loading the asset itself. – sarnold May 15 '11 at 01:12
  • 1
    You answered your own question. Cheat. – Raynos May 15 '11 at 01:16
  • 3
    Regarding valid HTML, it's apparently a tradeoff between rendering cost and communication cost. For instance, Google is notorious for not serving valid HTML, because it helps shave bytes off (and with their traffic, it adds up). – Amadan May 15 '11 at 01:20
  • Amadan has a valid point, Google's search markup is far from valid. – Razor May 15 '11 at 01:23
  • 7
    @Raynos: Nonsense. This is perfectly valid/acceptable on SO. SO is not a forum or something. – BalusC May 15 '11 at 01:27
  • @sarnold You're right that's poorly worded, let me rewrite that section – onteria_ May 15 '11 at 01:28
  • @Amadan Hmm, let me todo that and come back to it so I can formulate that into the response. – onteria_ May 15 '11 at 01:29
  • 1
    @Raynos, he answered it as "community wiki". @onteria_ could not possibly have done more to bend over backwards and *not gain rep*. What precisely are you accusing him of? It's quite common that the person most interested in the answer to the question (the OP) manages to discover an answer on his own. – Kirk Woll May 15 '11 at 01:40
  • 2
    @KirkWoll it was a tongue in cheek joke. – Raynos May 15 '11 at 01:42
  • @Raynos, fair enough. Sometimes text communication sucks. :) – Kirk Woll May 15 '11 at 01:45
  • 1
    Speeding up HTML pages with Valid HTML? As I understand it the Google homepage is *not* valid HTML because of performance. – Russell Dias May 15 '11 at 03:18
  • @Russel @Amadan already commented on this, and as noted I plan to add a section on rendering versus network performance. The main issue is, when does poorly written HTML rendering performance outweigh the gain of network performance. – onteria_ May 15 '11 at 03:28
  • @oneria: Ah, my mistake. I must have overlooked his comment. Cheers – Russell Dias May 15 '11 at 08:55
  • @onteria_ - Valid/invalid HTML, except possibly in one or two corner cases, won't affect pages rendering times at all. It might affect page parsing times, but looking at the HTML5 parsing algorithm, it hard to see where validity would make a measurable difference. Can you provide evidence of non-validity slowing a page down, or link to a scientific study of the issue? – Alohci May 15 '11 at 09:43
  • 1
    I've taken out the valid HTML section, since if I sat down and really put effort into it, I could show cases where it matters in rendering times, but such profiling is difficult considering browser caching and tools available. That's more time that I'm unfortunately willing to dedicate. With regards to people mentioning Google, Google has very well paid brilliant minds working for it, and knows how to shave off bytes here and there to make their pages render/work well in a large variety of browsers. Your average John and Jane web programmer won't have that scale of resources. – onteria_ May 15 '11 at 10:56
3

Minify your HTML source, CSS source, and JS Source. Gzip if possible.

http://code.google.com/p/htmlcompressor/

For JavaScript try: http://code.google.com/closure/compiler/

Pwnna
  • 9,178
  • 21
  • 65
  • 91
  • Oh god, minified HTML :( Then I can't read other people's source anymore. – Raynos May 15 '11 at 02:55
  • Well you do what you have to do. I believe there are unminifier.. or magnifier... (hey maybe i'll call mine that). Or just.. read it with any XML library, then use the print pretty XML function. Or use dev tools in Opera or Chrome (firebug does it, too, i believe.) – Pwnna May 15 '11 at 02:59
  • @Raynos - Google a prettify html tool. – Kit Sunde May 15 '11 at 04:03
  • @KitSunder it's not the same as right click view source. It's just far too much effort. – Raynos May 15 '11 at 04:04
  • Right click -> inspect element. – Pwnna May 15 '11 at 04:17
1

To start off, you should use a tool such as YSlow (Firefox and Chrome extensions available) or Googe Page Speed Online. There are others are out there I'm sure. These tools will grade your sites performance in different areas and provide tips on how you can improve them.

After using these tools for a while you'll start to change the way you build your pages and factor in these extra steps.

You could also look at async script loaders for your JavaScript files. A popular one is head.js. A search on Google should give you tones more articles on more in depth techniques such as this.

Razor
  • 17,271
  • 25
  • 91
  • 138
0

Mandatory reference.

Best Practices for Speeding Up Your Web Site

Trufa
  • 39,971
  • 43
  • 126
  • 190
-1

use PHP's flush function after your head tag

<html>
<head>
</head>
<?php flush(); ?>
<body>
</body>
<html>
dockeryZ
  • 3,981
  • 1
  • 20
  • 28
  • I'm curious, what's the performance gain of this? Also I probably should have clarified this was specific to front end optimizations, since backend effects on page loads can be more complicated (database connections, caching, etc.) – onteria_ May 15 '11 at 01:34
-1

The HTML5 boilerplate will get you far without any sweat.

Sindre Sorhus
  • 62,972
  • 39
  • 168
  • 232
  • 5
    The boilerplate will get you up and running sure, but it will by no means optimize the pages you build on top of it. – Razor May 15 '11 at 01:35