views:

75

answers:

4

I was just wondering if there was a professional opinion on the matter of compact code. Does it really speed up page loading. Example:

<body>
<div id="a"></div>
<div id="b"></div>
</body>

VS

<body>

<div id="a"></div>

<div id="b"></div>

</body>

any ideas?

+7  A: 

Yes, compact code speeds up page loading due to decreased payload...but not by a measurable amount, at least in most cases, unless your page is massive you won't see a difference.

Pages should be delivered via gzip, making the size difference between spaced and un-spaced negligible, just do what's readable to you, you'll thank yourself later. As with everything, you have to weight the costs, in this case a very minor difference in payload size, with what's easiest to maintain for you.

Nick Craver
I agree, my understanding is that GZIP creates a dynamic library of words. The result is that if you use the word <div> three hundred times in your HTML, only one <div> is sent to the browser with 299 pointers to the library. This makes the compact/expanded code issue moot because GZIP is converting what you see into a library and pointers.
Christopher Altman
Thanks for the info, never knew about gzip or deflate. Am I right in saying that you need to specify the deflate command in an .htaccess file? Cheers :-)
William Hand
@William - That's correct, take a look [here](http://www.jhuskisson.com/articles/adding-gzip-compression-to-your-site-via-htaccess) and [here](http://www.samaxes.com/2008/04/htaccess-gzip-and-cache-your-site-for-faster-loading-and-bandwidth-saving/) for more details :)
Nick Craver
Thanks guys, I understand now.
William Hand
@William - Make sure to [accept answers](http://meta.stackoverflow.com/questions/5234/how-does-accepting-an-answer-work), it helps get answers to your questions faster and from a wider audience :)
Nick Craver
A: 

in your example, it saves up 3 bytes of code... so i don't think it has any noticeable effect on page loading time in modern times and it's internet speed. a better improvement would be to send your page gziped.

oezi
+3  A: 

In theory, yes.

For the server, if it has to send out a 1MB file to each client, it has to spend n amount of time and resources sending out that one file. Now, if you were able to cut the file size in half, the time and resources it would take per user on the server would be .5n.

For the client, it has to download a file. Assuming a download rate of 25KB/S, a 1MB file would take 41 seconds to download. A .5MB file would take 20.5s. Thats a savings of 20 seconds by reducing the file size.

However, in practice. No, I would not worry about it, unless you're dealing with audio/video/picture data. That's because a character in a HTML document is only a couple bytes. Sure, you might have lets say 100 extra characters that you could trim and remove - whitespace for instance. At most you'd save up an additional 1KB per page.

I wouldn't be too concerned about it, unless you're developing an application or solution where it needs to be compact. But any modern or sub-modern computer won't break with 1KB extra data in their HTML file.

Jeffrey Kern
Thanks for the answer. Its appreciated.
William Hand
A: 

Page loading and compact code ? yes it really make things better as additional newlines and spaces are nothing but characters which need to be downloaded on the client end.

However i will suggest you to see it as part of the big strategy for optimization.

I will suggest you to take a look at YSlow/Yahoo Guidelines which will help you understand the different parts of "strategy" which is added to server and client components also. And collective results are just amazing for big sites.

Anil Namde