~ / writing / tips and tricks

Tips and Tricks · May 2024

What is Image Lazy Loading and Why Should We Use It?.

Lazy loading of images is a technique for loading images on a web page only when they are required.

Amit Tiwari Tips and Tricks 4 min read

Lazy loading of images is a technique for loading images on a web page only when they are required. This method can improve the page’s loading time without reducing its size. This technique improves overall page performance without compromising page size. To improve page loading speed, we must use Asynchronous ( Async ) and Deferred ( Defer ) attributes in our website’s script execution.

That brings us to the async and defer attributes, and to the problem they exist to solve. Render-blocking resources are one of the most common findings when you test a site, and page speed insight flags them on almost every WordPress install.

How to Fix Render-Blocking Resources in WordPress

When a website is loading, it downloads and executes multiple components at the same time, including HTML, CSS, and JavaScript files. However, a common issue arises when JavaScript files are executed, causing delays in the rendering and parsing of HTML files. This phenomenon, known as render-blocking resources, is frequently identified as a problem when assessing website performance using tools such as PageSpeed Insight.

The fix starts with the “async” attribute on your script tags. With “async” in place the JavaScript loads asynchronously, so rendering carries on while the file downloads and runs. The page stops stalling on scripts it does not need yet, which is what the performance guidance has been asking for all along.

PageSpeed Insights opportunities list with an arrow pointing at Properly size images and its estimated saving

What is Difference Between Async & Defer in Script Loading 

When a website is accessed, multiple files, including JavaScript and HTML, are downloaded and processed simultaneously. During this phase, the browser is responsible for interpreting and applying CSS styles, as well as running JavaScript code. The moment a JavaScript file arrives and runs, though, it stops the browser rendering and parsing your HTML. That is what “render-blocking resources” means: the scripts hold the page hostage while everything else waits. It shows up plainly in performance tools such as PageSpeed Insight, which will tell you to “eliminate render-blocking resources.” The usual answer is the ‘async’ attribute on the script tag, which lets the JavaScript load in parallel and gets the page rendering sooner.

The ‘defer’ attribute works similarly, allowing for parallel parsing of HTML content while simultaneously downloading JavaScript files. The ‘defer’ attribute ensures that JavaScript execution is postponed, as opposed to non-deferred JavaScript’s immediate execution, which can disrupt HTML parsing. As a result, the JavaScript files are downloaded in the background and stored in the browser’s cache, without interfering with the ongoing processing of the HTML document. The execution of the deferred JavaScript files is strategically delayed until the HTML document is fully parsed. This method significantly speeds up the rendering of HTML content, ensuring that the webpage is visible to the user on time. The deferred execution of JavaScript prioritizes the immediate display of content, with JavaScript functionalities initialized after the HTML structure has been fully loaded and rendered, ensuring an optimized page-loading experience without jeopardizing the HTML parsing process.

Table comparing async and defer script loading against download, HTML parsing and execution behaviour

Which Images Can Be Lazy Loaded, and Which Can Be Preloaded?

To improve your website’s speed, use a specific approach to loading images, particularly those displayed in the first visible section of a webpage, known as “above the fold.”

However, an exception is advised for the images situated in the top section, or above the fold content. For these images, it is more efficient to pre-load them. Pre-loading involves initiating the download of these images in advance, ensuring they are already present on the user’s device when the webpage is first accessed. This pre-emptive loading minimizes any waiting time for the user, providing a faster and smoother experience.

To improve website performance and user experience, a strategic approach to image loading is necessary. Images in the “above the fold” section, or visible area before scrolling, should be prioritized for immediate loading. Preloading these images ensures that they are fetched and rendered immediately upon page load, providing an uninterrupted viewing experience. Images below the fold, on the other hand, benefit from lazy loading, which defers their download until they reach the viewport. This method significantly reduces the initial load time, saving bandwidth and increasing page speed.

Best & Fastest Lazy Load Plugins in WordPress

Flying Press:

Choosing Flying Press to lazy load your images is a smart move for making your website faster. It makes your site load faster by only bringing in images when they are required, saving time for your visitors. Flying Press is simple to use, works with a variety of website platforms, and ensures that your important images load quickly. So, it’s an excellent choice for speeding up your website and making it more user-friendly.

WP Rocket : 

WP Rocket is an excellent choice for lazy loading your images. It’s simple to use, whether you’re a beginner or a professional. WP Rocket accelerates your website by only loading images when visitors are about to see them. This means your website will load faster for visitors. It’s like a magic trick that speeds up your website and makes it great for everyone.

Discuss in the community ↗