性能:绝对与相对URL [英] Performance: Absolute vs. Relative URLs
问题描述
什么更快? 热链接(内联链接)到绝对URI 或自己承载资源并使用相对URI ?
What's faster? Hot linking (inline linking) to an absolute URI or hosting the resource yourself and using a relative URI?
In his tutorial on how to style HTML5 elements in Internet Explorer, Remy Sharp states that hot linking causes an "extra HTTP [GET] request." I agree if you're comparing hot linking to copying & pasting (embedding) the script into the HTML. But, if you're comparing hot linking to hosting the script locally and linking via a relative path, then I'd argue that hot linking is actually (ever-so-slightly) faster because the browser doesn't have to resolve the absolute URL from the relative path. In both cases, however, an extra HTTP GET request is performed, correct?
推荐答案
正确的答案是 - 这取决于。
The correct answer is - it depends.
热链接可能很慢,因为 -
Hotlinking can be slow because -
- 需要额外的DNS查找
- 无法重用现有的TCP / IP套接字连接
服务器上的托管可能很慢因为 -
Hosting on your server can be slow because -
- 浏览器每个主机只允许n个并发请求。再向同一主机发出一个请求有可能引入排队,这可能很慢。数字'n'是浏览器特定的,介于2到6之间。请参阅browserscope>网络>每个主机名的连接。
- Browsers only allow n concurrent requests per host. Having one more request to the same host has the potential to introduce queuing, which can be slow. The number 'n' is browser specific, and is anywhere between 2 and 6. See browserscope > network > connections per host name.
如果你认为两台服务器在各方面都相同,我会说服务器上的托管速度会更快。在每个主机的连接数为6的新浏览器中尤其如此。
If you assume both servers are identical in every respect, I'd say hosting on your server is going to be faster. This is true especially on new browsers where the number of connections per host is 6.
但遗憾的是,事情从未如此简单。我建议仅在以下情况下使用热链接 -
But sadly, things are never so simple. I'd recommend using hotlinking only if -
- 您的域名资源太多(图片/ js)
- 另一台服务器是CDN,资源非常流行,因此浏览器的缓存中存在很大的机会。在谷歌的服务器上思考JQuery。
对于所有其他用例,最好在自己的服务器上托管。
For all other use cases, you are better off hosting on your own servers.
这篇关于性能:绝对与相对URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!