Fast enough? 9 tips for web page optimization

Felipe Farias
September 22, 2015
<p>The world is large, and the internet isn't fast everywhere. Web optimization needs to ride along with user needs. Don't waste your time with performance if it will not enhance user experience. Instead, try to study how users interact with your application. Ask yourself: what is the first thing a user needs to see on my page? Does (s)he see it? If it doesn't match, it is time to make some improvements.</p><p>So here are some optimizations to keep in mind:</p><h2 id="1-avoid-inline-js-css-files-">1. Avoid Inline JS/CSS Files:</h2><pre><code>&lt;h1 style="color:blue;margin-left:30px;"&gt;This is a heading.&lt;/h1&gt; </code></pre><p>It's a bad practice to write inline code. Not only because code is more difficult to read and maintain, but also because of performance. Browsers can't cache inline JS/CSS. So every time users access your page, the content will be downloaded.</p><h2 id="2-avoid-unnecessary-plugins-">2. Avoid Unnecessary Plugins:</h2><p>Do you need this plugin? jQuery is crucial on this project? Analyze your plugins and try to remove what is not necessary. If you use a responsive front-end framework like <a href="">Bootstrap</a> or <a href="">Foundation</a>, keep in mind that it has a lot of stuff. Download a customized version only with the main components you need.</p><h2 id="3-avoid-http-requests-concatenating-files-">3. Avoid HTTP Requests concatenating files:</h2><p>Always avoid unnecessary requests. See the example below:</p><pre><code> </code></pre><p>Merge the files:</p><pre><code> </code></pre><p>Use concatenation tools to merge files and minification to reduce file sizes. A good way to integrate concatenation and minification in your projects is <a href="">Grunt</a>. It is a JavaScript task runner with literally hundreds of plugins to choose from. Specifically for concatenation and minification it has <a href="">grunt-contrib-concat</a> (concatenation) and <a href="">grunt-contrib-uglify</a> (minification).</p><h2 id="4-javascript-only-at-the-end-">4. JavaScript Only at the End:</h2><p>JavaScript blocks additional resources from being downloaded. Putting it at the bottom of the document will let all other elements (like images and CSS) load, giving the perception of improved performance.</p><h2 id="5-async-vs-defer-attributes">5. async vs defer attributes</h2><p>The HTML <code>&lt;script&gt;</code> element allows you to define when the JavaScript code on your page should start executing.</p><p>The HTML file is rendered right after it has finished the download.</p><pre><code>&lt;script src="sample.js"&gt;&lt;/script&gt; </code></pre><figure class="kg-card kg-image-card"><img src="" class="kg-image" alt="web-opt-script1.png"></figure><p><code>async</code> scripts will be executed asynchronously as soon as it is available. It will pause the HTML parser to execute it when it has finished downloading.</p><pre><code>&lt;script async src="sample.js"&gt;&lt;/script&gt; </code></pre><figure class="kg-card kg-image-card"><img src="" class="kg-image" alt="web-opt-script2.png"></figure><p><code>defer</code> downloads the file during HTML parsing and will only execute it when the page has finished rendering.</p><pre><code>&lt;script defer src="sample.js"&gt;&lt;/script&gt; </code></pre><figure class="kg-card kg-image-card"><img src="" class="kg-image" alt="web-opt-script3.png"></figure><p><strong>If you need to support IE9</strong>, don't use <code>defer</code> in your project and include your scripts with no attribute if execution order is a concern.</p><h2 id="6-appropriate-images-for-each-device-">6. Appropriate images for each device:</h2><pre><code>//Lower-resolution images @media (max-width:440px) { .image { background-image: url(image-lower.jpg); } } //Hi-resolution images @media (min-width:441px) { .image { background-image: url(image-hi.jpg); } } </code></pre><p>The idea is to have users downloading only those images that are best suited for their devices. In the case of mobile devices, this would be lower-resolution images that can be quickly rendered.</p><h2 id="7-css3-instead-of-images-">7. CSS3 Instead of images:</h2><p>Programmers tend to use more images than necessary.</p><figure class="kg-card kg-image-card"><img src="" class="kg-image" alt="buttons-example"></figure><p>A lot of elements like buttons, rounded corners, shadows, gradient, etc., can be rendered by CSS3, reducing the number of HTTP requests and speeding uploading time.</p><h2 id="8-image-sprites">8. Image Sprites</h2><p>The idea behind sprites is to combine images into a single image file, reducing the number of HTTP requests.</p><figure class="kg-card kg-image-card"><img src="" class="kg-image" alt="sprites"></figure><p>The image is displayed by using the CSS <code>background-position</code> property.</p><pre><code>&lt;ul&gt; &lt;li class="img-amazon"&gt;&lt;/li&gt; &lt;li class="img-meteor"&gt;&lt;/li&gt; &lt;li class="img-angular"&gt;&lt;/li&gt; &lt;/ul&gt; </code></pre><pre><code>//Defining image size. .ul .li { background-image: url(""); width: 150px; height: 150px; } //Optional class. .img-amazon { background-position: 0; } .img-meteor { background-position: 150px 0; } .img-angular { background-position: 300px 0; } </code></pre><p>For more information about <code>background-position</code> access <a href="">MDN</a>.</p><h2 id="9-testing-performance">9. Testing Performance</h2><p>Web optimization tools for reference:</p><ul><li><strong><a href="">PageSpeed:</a></strong><br>PageSpeed tools analyze and optimize your site following web best practices.</li><li><strong><a href="">Yahoo! YSlow:</a></strong><br>YSlow analyzes web pages and why they're slow based on Yahoo!'s rules for high-performance web sites</li></ul><p>Do you like to join the discussion? Please, leave your comments below.</p>