Using Data URIs can negatively impact the loading speed of a webpage

I created a custom script to replace all inline images with data URIs in order to minimize http requests and speed up loading times on mobile devices.

Surprisingly, I noticed a decrease in loading speed. Could it be because the HTML file was larger (around 100kb instead of the usual 5kb)? Or is there another factor with data URIs that is causing the slowdown?

Does the browser need to fully download the entire document before loading linked resources within it? Or will linked resources such as CSS and JavaScript at the top of the document be loaded before the browser finishes downloading the entire document?

How does the browser handle CSS? Does it need to load the entire CSS file before processing all the CSS settings?

If so, would it be more efficient to have a separate CSS file for data URIs like this:

  1. Load CSS for structure (without data URIs)
  2. Load CSS for background images (using URI format for all background images)

Would a "separate cached jpg file" load quicker than a "URI-based image included in a cached CSS file"?

Any other recommendations on optimizing the use of data URIs?

Answer №1

mobify.com recently shared an interesting finding in their latest blog post: New Research Shows Data URIs are 6 Times Slower than Source Linking on Mobile

[…]
Surprisingly, after analyzing the performance of a large number of mobile page views, it was revealed that the loading time of images using data URIs is, on average, 6 times slower than when utilizing a binary source link like an img tag with an src attribute!

While not delving deeply into the technical aspects, one possible explanation could be the higher CPU power required for decoding base64-encoded data URIs.

It is recommended by the author to use data URIs sparingly, especially for small images.

Answer №2

When HTML content is loaded, it follows the order in which it appears in the HTML file. Using data URI's that are large in size can decrease the speed of the page loading process. It's important to note that large data URIs are not supported in IE browsers, especially IE6.

It is recommended to use data URIs for images that are less than 20KB in size, as this is the norm.

To improve the speed of your page loading, you can consider options like image compression, JavaScript optimization, and CSS compression.

Answer №3

Summarized: the use of data URIs can delay the loading of HTML if the images are large

I created a script that replaced inline images with data URIs to decrease http requests and speed up loading times on mobile devices.

It's a viable option if the time taken to make requests is longer than downloading the images, as the browser will need to download the image data regardless (whether through a data URI or not). Embedding images as data URIs in the HTML will increase the overall size of the HTML by the combined size of all images (plus approximately 33% for data URI encoding).

For example, if establishing a new connection takes 1s, downloading each image takes 0.2s, there are 10 images, and the HTML download time is 0.5s, the calculations would be:

  • 1s + 0.5s = 1.5s for HTML alone
  • 10 x (1 + 0.2)s = 12s for images

If the images are encoded as data URIs:

  • 1s + 0.5s + (10 x 0.2s x 1.33) = 4.2s
  • (without external requests for images)

The total time taken to fetch the entire HTML source is crucial (1.5s vs 3.5s). If instant image display is vital, data URIs are beneficial. However, if loading most of the page first and then images asynchronously is acceptable, prioritizing complete HTML download may be more effective.

Additionally, encoding large images as data URIs can worsen performance. If each image takes 1 second to download:

For external images:

  • 1s + 0.5s = 1.5s for HTML alone
  • 10 x (1s + 1s) = 20s for images

Using data URIs:

  • 1s + 0.5s + (10 x 1s x 1.33) = 14.8s until HTML completion!

Could the larger html file size (around 100kb instead of 5 kb) impact this decision?

As the browser downloads the HTML, it begins interpreting and displaying it. External resources such as images with regular URIs and (async) scripts are fetched while continuing to process the HTML.

The user sees more content gradually as the page loads, even before images or fonts are fully loaded.

However, when encountering a data URI image tag, the browser must download and parse the entire data URI string, creating a pause as it processes the large encoded image before proceeding with the HTML.

Does the browser need to finish downloading the entire document before loading linked resources within it?

Linked resources have independent loading and parsing behavior. <script> tags are loaded synchronously, blocking HTML parsing until execution, unless marked as async. External stylesheets load in parallel but delay rendering until complete.

This link provides further insight.

Is it better to separate CSS with data URIs into a distinct file?

  1. Load CSS for structure (without data URIs)
  2. Load CSS for background images (all in URI format) Is a "separate cached jpg file" quicker than a "URI based image within a cached CSS file"?

Any other suggestions for utilizing data URIs effectively?

The choice depends on your optimization goals. Prioritize initial rendering speed, even if the page is incomplete, or aim for full page rendering with all images included?

There's no one-size-fits-all rule for using data URIs. Consider applying them for small images like icons. Webpack's url-loader automates this process, utilizing data URIs for smaller files and external URLs for larger ones.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the process for extracting the differences between two or three files and merging them together in Node.js?

Recently, I've been experimenting with webpack and successfully managed to output and transform es5 build to include nomodule in the tags directly from HtmlWebpackPlugin using hooks. Similarly, for the es6 build, I added type="module". Howe ...

Is there a way to provide "only responsive" content to the mobile m. subdomain without the need for redirection?

I currently have a website with www. that redirects to different content on the mobile m. site. I am in the process of updating my site to be responsive, but I want to ensure that I do not lose the links to the m. site in Google SERP. How can I redirect m ...

Changing Background Colors of Grid Items in React.js Grid Layout

I'm currently learning React and have created a basic app that shows data from a database in a grid format. I'm looking to customize the background colors of each grid item based on its priority level, which is pulled from the database. The prior ...

Preventing the collapse of the right column in a 2-column layout with Bootstrap 3

I've recently begun exploring Bootstrap and am attempting to achieve the following: (Left column - larger) Heading Heading 2 Heading Image Heading Image Heading Address (Right column - smaller) Heading Paragraph List items My issue ...

Slider with Dual Images: A Visual Comparison

I'm currently working on a webpage that features before and after images using a slider based on mouse movement to display both pictures. I am trying to incorporate multiple sliders on the same page but have been facing difficulties in getting them to ...

Android not showing scroll bar in ion-scroll feature

I am experiencing an issue where a div with a fixed height displays a scroll bar on browsers but not on an Android phone. <ion-scroll style="height:300px;" scrollbar-y='true'> //content </ion-scroll> ...

Is there a way to assign the scroll event context to `document.body` instead of `window`?

Discovery As I delved into the intricacies of web development, I stumbled upon a curious observation. In a particular MDN page, I noticed that the left and right sidebars had their scrollbars contained within themselves. However, the content block's ...

Can you explain the variance between e-resize and ew-resize cursor styles?

Can you explain the differences between these cursor types? I'm noticing that they all appear as the same cursor in Chrome on Windows. .e-resize {cursor: e-resize;} .ew-resize {cursor: ew-resize;} .w-resize {cursor: w-resize;} <p>Mouse over t ...

Troubleshooting collapsing problems in Bootstrap 5.2 using Webpack and JavaScript

I am currently developing a project utilizing HTML, SASS, and JS with Webpack 5.74.0. My goal is to implement a collapsible feature using Bootstrap 5, however, I ran into some issues while trying to make it work. To troubleshoot, I attempted to direct ...

Using R programming language to download PDF files from a website through web scraping

Looking for assistance in R coding to automatically download all the PDFs linked on this URL: . The goal is to save these PDFs into a designated folder. I've attempted the following script with guidance from , however, it's resulting in errors: l ...

What steps do I need to take in order to show the present value using a range input?

Hey there! I'm working on a code in HTML that includes an input of type range. Here's what it looks like: This is my input: <input type="range" min="1" max="100" value="50" class="slider" id="myRange"> Unfortunately, I'm unable to s ...

Jquery Droppable issue arising with dynamically added DIVs

I am facing a similar issue as described in this question and this one I am trying to implement drag-and-drop and resize functionality. It is working fine for static elements, but I encounter issues when adding dynamic divs. The resize property works prop ...

The event tooltip in fullcalendar does not show up as expected

Within my Laravel 5.8 / Bootstrap v4.1.2 / jQuery jQuery v3.3.1 fullcalendar v4.3.1 application, I am attempting to incorporate tooltips for events in the fullcalendar plugin. To achieve this, I referred to a specific example provided here: Sample Example ...

Using Cordova to create an accordion list on an HTML webpage

I am in need of an accordion list implementation for the given HTML code. I specifically want it to expand when 'Technical' is clicked, and collapse upon clicking again. Here is the HTML code: <!-- Techincal --> <div class= ...

CSS smoothly scrolls upward within a set height restriction

Is there a way to use CSS to ensure that the scroll bar of a fixed-height message box is always at the bottom, displaying the latest entry? Currently, as more messages populate the chat box main section, everything inside remains static. The only way to v ...

Prevent a div from being displaced by the transform property once it reaches the window's border

Is it possible to prevent the viewer I created using CSS transform from moving when its borders reach the window borders? Should I consider using a different method instead? If you'd like to see the code, you can check it out here. var x=0, y=0 ...

Textbox value disappears after being updated

When I click on the edit link (name), the value from the database is displayed as a textbox. However, when I update another normal textbox (age), the value in the edit link textbox disappears. Strangely, if I input a new value into the edit link textbox, i ...

A set of six DIV's, divided into two columns of three each

I'm attempting to arrange two columns with three vertical DIVs side by side. The left column is designated for main text, while the right column is reserved for miscellaneous information. Each column consists of a top and bottom DIV that display image ...

What could be the reason for my flex items not shrinking when the browser window size is decreased?

Here is the CSS code for the header component using flexbox: .header { display: flex; flex-direction: row; justify-content: space-between; padding: 15px 20px; background-color: #ffffff; position: sticky; top: 0; z-index: ...

What is the trick to having the ball change colors every time it hits the wall?

Recently, I stumbled upon this interesting ball bouncing question while searching for some JavaScript code. While the basic bounce animation was simple to achieve, I started thinking about how we could make it more dynamic by changing the color of the ball ...