Is It More Efficient to Put Raw Image Data in the Src Attr?

拥有回忆 提交于 2019-12-04 03:59:42

It depends on what you mean by "more efficient". If your measure is time, then it can be more efficient.

The technique you're referring to is using a data URI. Typically you take the image data and base64 encode it so it contains only ASCII characters. base64 encoding data has the effect of making it 33% larger (every 6 bits become 8).

So this works for small images, but for large ones, the 33% premium may be too much.

The reason why it might be a good idea is that latency is often the limiting factor for browser requests. It used to be (back in the day) that bandwidth was the restriction, so the common advice was to split up your resources, but that's not true anymore. With a data URI image, the browser doesn't have to make a second round trip.

Aside from all that, you have to consider browser support. Prior to version 8, IE has no support for data URIs. And in IE 8, there's an upper limit of 32KB of data.

Hope this helps!

There is a size limit to this. I don't know for certain off the top of my head, but 2K seems about right.

Remember that there is overhead for base64 encoding something. If you have a 500 byte image, this might be fine, but for other things, no.

Really though, you shouldn't be doing this right now for compatibility. Maybe in the coming years...

It depends on how many images you're going to be sending and how often they get requested. Having the images in base 64 is absolutely more efficient then 30 http requests.

You could also implement caching of each image if they get requested frequently. This is something we've implemented in my workplace. We store the base64 in a temp directory and check if they've already been encoded. If so, we have an even faster response time, otherwise they get created on the fly in the PHP script. The more popular pages will be warmed up in the cache very quickly.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!