为网站使用内联/base64 图像比仅链接到硬文件快多少? [英] How much faster is it to use inline/base64 images for a web site than just linking to the hard file?

查看:40
本文介绍了为网站使用内联/base64 图像比仅链接到硬文件快多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与简单地链接到服务器上的硬文件相比,使用 base64/line 显示图像要快多少?

How much faster is it to use a base64/line to display images than opposed to simply linking to the hard file on the server?

url(data:image/png;base64,.......)

我无法找到任何类型的性能指标.

I haven't been able to find any type of performance metrics on this.

我有几个问题:

  • 您不再获得缓存的好处
  • base64 的大小不是比 PNG/JPEG 文件大很多吗?

让我们将更快"定义为:用户看到完整呈现的 HTML 网页所需的时间

推荐答案

更快"是一个很难回答的问题,因为有很多可能的解释和情况:

'Faster' is a hard thing to answer because there are many possible interpretations and situations:

Base64 编码会将图像扩展三分之一,这将增加带宽利用率.另一方面,将它包含在文件中将删除另一个 GET 往返服务器.因此,与使用不同的图像文件相比,具有高吞吐量但低延迟的管道(例如卫星互联网连接)可能会更快地加载具有内联图像的页面.即使在我的(农村、慢速)DSL 线路上,需要多次往返的站点也比那些相对较大但只需要几个 GET 的站点加载时间要长得多.

Base64 encoding will expand the image by a third, which will increase bandwidth utilization. On the other hand, including it in the file will remove another GET round trip to the server. So, a pipe with great throughput but poor latency (such as a satellite internet connection) will likely load a page with inlined images faster than if you were using distinct image files. Even on my (rural, slow) DSL line, sites that require many round trips take a lot longer to load than those that are just relatively large but require only a few GETs.

如果您对每个请求的源文件进行 base64 编码,您将使用更多 CPU,破坏您的数据缓存等,这可能会损害您的服务器响应时间.(当然你总是可以使用 memcached 之类的来解决这个问题).

If you do the base64 encoding from the source files with each request, you'll be using up more CPU, thrashing your data caches, etc, which might hurt your servers response time. (Of course you can always use memcached or such to resolve that problem).

这样做当然会阻止大多数形式的缓存,如果经常查看图像,这可能会造成很大的伤害 - 例如,显示在每个页面上的徽标,通常可以由浏览器缓存(或代理缓存像鱿鱼或其他东西)并要求每月一次.它还将阻止 Web 服务器使用 sendfile(2) 等内核 API 提供静态文件的许多优化.

Doing this will of course prevent most forms of caching, which could hurt a lot if the image is viewed often - say, a logo that is displayed on every page, which could normally be cached by the browser (or a proxy cache like squid or whatever) and requested once a month. It will also prevent the many many optimizations web servers have for serving static files using kernel APIs like sendfile(2).

基本上,这样做在某些情况下会有所帮助,而在其他情况下会受到伤害.您需要确定哪些情况对您很重要,然后才能真正确定这对您来说是否值得.

Basically, doing this will help in certain situations, and hurt in others. You need to identify which situations are important to you before you can really figure out if this is a worthwhile trick for you.

这篇关于为网站使用内联/base64 图像比仅链接到硬文件快多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆