9

Some webpages bake image data into the HTML, like this one which has baked it into a piece JavaScript code, making the size of the HTML document itself reach several MB in size. Why do some web developers choose to do that? What are the benefits?

12

There is a similar question on Stack Overflow.

What you are seeing is referred to as a data URI scheme but some just call it a base64 encoded image. The main benefit can be page speed but there are various reasons (shown below) as to when it's more appropriate.

According to Wikipedia:

Advantages:

  • HTTP request and header traffic is not required for embedded data, so data URIs consume less bandwidth whenever the overhead of encoding the inline content as a data URI is smaller than the HTTP overhead. For example, the required base64 encoding for an image 600 bytes long would be 800 bytes, so if an HTTP request required more than 200 bytes of overhead, the data URI would be more efficient.

  • For transferring many small files (less than a few kilobytes each), this can be faster. TCP transfers tend to start slowly. If each file requires a new TCP connection, the transfer speed is limited by the round-trip time rather than the available bandwidth. Using HTTP keep-alive improves the situation, but may not entirely alleviate the bottleneck.

  • When browsing a secure HTTPS web site, web browsers commonly require that all elements of a web page be downloaded over secure connections, or the user will be notified of reduced security due to a mixture of secure and insecure elements. On badly configured servers, HTTPS requests have significant overhead over common HTTP requests, so embedding data in data URIs may improve speed in this case.

  • Web browsers are usually configured to make only a certain number (often two) of concurrent HTTP connections to a domain, so inline
    data frees up a download connection for other content.

  • Environments with limited or restricted access to external resources may embed content when it is disallowed or impractical to reference it externally. For example, an advanced HTML editing field could accept a pasted or inserted image and convert it to a data URI to hide the complexity of external resources from the user. Alternatively, a browser can convert (encode) image based data from the clipboard to a data URI and paste it in a HTML editing field. Mozilla Firefox 4 supports this functionality.

  • It is possible to manage a multimedia page as a single file. Email message templates can contain images (for backgrounds or signatures)
    without the image appearing to be an "attachment".

Disadvantages:

  • Data URIs are not separately cached from their containing documents (e.g. CSS or HTML files) so data is downloaded every time the
    containing documents are redownloaded. Content must be re-encoded and re-embedded every time a change is made.

  • Internet Explorer through version 7 (approximately 15% of the market as of January 2011), lacks support. However this can be overcome by serving browser specific content. Internet Explorer 8 limits data URIs to a maximum length of 32 KB.

  • Data is included as a simple stream, and many processing environments (such as web browsers) may not support using containers (such as multipart/alternative or message/rfc822) to provide greater complexity such as metadata, data compression, or content negotiation.

  • Base64-encoded data URIs are 1/3 larger in size than their binary equivalent. (However, this overhead is reduced to 2-3% if the HTTP
    server compresses the response using gzip) Data URIs make it more
    difficult for security software to filter content.

According to other sources

  • Data URLs are significantly slower on mobile browsers.

Credit to Shaz for his answer here

| improve this answer | |
  • 5
    Internet Explorer through version 7 (approximately 15% of the market as of January 2011) - is that... relevant today? – Vilx- Oct 3 at 15:30
  • 1
    Another advantage is that if everything is contained in a single file, a "Save As..." will produce a simple local copy of the page that can be easily transferred as a single unit via USB drive or other means, and can be moved around without having to also drag around a directory of supplemental files to go with it. – supercat Oct 3 at 16:39
  • Additional drawback that ties in with the caching bullet point but is also a broader separate issue: the image is not (easily) re-usable on another page. I.e. a company logo that occurs on each page will be downloaded with each page instead of once initially. – Frank Hopkins Oct 3 at 18:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.