Are you using tabs or spaces to indent your markup? Does it matter for performance which one you chose? Let’s run an experiment. Consider a page that generates a list of 50 items: The code generates a long list of <li> elements and keep their indentation using the editor’s settings for tabs, spaces and tab size. Default in many editors is spaces and a tab size of 4. With spaces it looks like this: and with tabs it looks like this: It’s clear to see that the indentation takes up 4 characters when spaces are used and only a single character when using tabs. If we then compare the total file size of the two variations, here’s what we get: Using tabs saves close to 18% of the file size over spaces. This is, however, not a true picture of a web page. All modern web servers use compression in form of GZip or Deflate before serving HTML to the browser. So let’s look at the numbers after GZip: When using GZipping, the saving from using tabs over spaces is just 1.5%. It’s still a saving and it counts. Yet again, this is not the complete story because some web developers make sure to minify the HTML by removing redundant whitespace, unneeded quotation marks etc. Normally this is done as a build step or at runtime. So let’s minify the HTML and see what results it produces: When minified, it doesn’t matter if tabs or spaces are used, since they are all stripped away. Depending on the capabilities of your server, build setup, runtime etc., here’s a little chart of what to do based on the above findings: Keep in mind that this is a controlled experiment, so your mileage will vary. If you want to enforce your entire team to use either tabs or spaces, then take a look at .editorconfig. There’s a plugin available for practically all editors. In the next segment, we look at the effects of GZipping vs. minifying HTML files.<ul>
@for (int i = 0; i < 50; i++)
{
<li>The count is @i</li>
}
</ul>
Tabs Spaces Saving Raw file size 1403 bytes 1703 bytes 300 bytes/18% Tabs Spaces Saving Raw file size 1403 bytes 1703 bytes 300 bytes/18% Raw file GZipped 327 bytes 332 bytes 5 bytes/1.5% Tabs Spaces Saving Raw file size 1403 bytes 1703 bytes 300 bytes/18% Raw file GZipped 327 bytes 332 bytes 5 bytes/1.5% Raw file minified 1199 bytes 1199 bytes 0 bytes/0% Minified & GZipped 312 bytes 312 bytes 0 bytes/0% Conclusion
Use tabs or spaces I can minify Doesn't matter at all I can GZip but not minify Doesn’t matter much (tabs gives small benefit) I can neither GZip nor minify Tabs
SEO1:Effects of GZipping vs. minifying HTML files
PublishedCreated: 2017年6月8日星期四 15:00:13 Latest updated:2020年9月30日星期三 23:06:14 views(1736)