简体   繁体   English

HTML行间距和紧凑代码

[英]HTML Line Spacing & Compact Code

I have the following example code: 我有以下示例代码:

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

If I add empty lines between each of my original lines, like this: 如果我在每个原始行之间添加空行,如下所示:

<body>

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

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

</body>

does that do anything to my site's performance? 这对我网站的表现有什么影响吗? Will the page load slower? 页面加载会变慢吗?

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. 页面应该通过gzip传递 ,使得间隔和不间隔的大小差异可以忽略不计,只要做你能读的东西,你以后会感谢你。 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. 正如一切,你必须加权成本,在这种情况下,有效载荷大小非常微小的差别,有什么是最容易为你维护。

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. 对于服务器,如果必须向每个客户端发送1MB文件,则必须花费大量时间和资源发送该文件。 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. 现在,如果您能够将文件大小减半,那么服务器上每个用户所需的时间和资源就是.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. 假设下载速率为25KB / S,则1MB文件下载需要41秒。 A .5MB file would take 20.5s. .5MB文件需要20.5秒。 Thats a savings of 20 seconds by reducing the file size. 通过减小文件大小可节省20秒。

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. 那是因为HTML文档中的字符只有几个字节。 Sure, you might have lets say 100 extra characters that you could trim and remove - whitespace for instance. 当然,你可能会说100个额外的字符,你可以修剪和删除 - 例如空格。 At most you'd save up an additional 1KB per page. 每页最多可节省1KB。

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. 但是任何现代或亚现代计算机都不会破坏其HTML文件中的1KB额外数据。

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. 在你的例子中,它节省了3个字节的代码...所以我不认为它对现代的页面加载时间有任何明显的影响,它是互联网速度。 a better improvement would be to send your page gziped. 更好的改进是发送您的页面gziped。

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. 我建议你看一下YSlow / Yahoo指南 ,它将帮助你理解添加到服务器和客户端组件的“策略”的不同部分。 And collective results are just amazing for big sites. 大型网站的集体结果令人惊叹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM