Minimizing HTTP Connections vs. Parallel Downloads

北城以北 提交于 2021-02-18 12:08:25

问题


For years, web developers have followed the logic that minimizing HTTP connections speeds up applications because the browser isn't choking on the download/execution of code. For example Yahoo has long touted their best practices, and tell us to combine CSS/JavaScript/image resources into single files - thereby reducing the total number of HTTP requests and compressing the total resource size.

But other "best practices" exist with regards to increasing webpage speed - specifically, maximizing the number of parallel HTTP downloads (from Google). This approach tells us that by spreading the HTTP connections across multiple hostnames the browser can do more simultaneously.

So as modern web applications are becoming very large (e.g. 3MB+ of JavaScript alone) the question must be asked:

Will my application load faster with 3MB+ of JavaScript in a single file? Or will it load faster with multiple, smaller files spread across hostnames?

For the sake of simplicity we should also assume other "best practices" are being followed, so this question best exists in a vacuum.

I have yet to see any empirical data on the subject, but I imagine there has to be a point where the performance of these approaches diverge - so knowing where that sweet-spot exists would be ideal.


回答1:


I think this depends on number of sockets available for the browser. Say the browser has it's 4 sockets available, 4 smaller requests will be faster than 1 large request.

The trick here would be knowing at startup what requests your application will send and maximize the # of requests for # of sockets a browser can use. I believe browsers only have 4 but to be honest I haven't ever looked to see if that number has changed in modern browsers.

Looks like each browser can have it's own number of sockets, some having 2: Max parallel http connections in a browser?

https://stackoverflow.com/a/985704/925782 says IE10 is winner with 8 sockets, wow, go IE :)

Cache control would also play part in this of course where first load would be everything, subsequent loads would be less actual requests.

If you want to get geeky: http://www.stevesouders.com/blog/2008/03/20/roundup-on-parallel-connections/

I agree that some charts and real data would be a great blog post, my response is purely theoretical in nature.




回答2:


I would pick parallel downloads.

Smaller JS files can be parsed faster than one monster-sized package. In most of the cases you do not need all of the JS at once either.

Concatenating assets is considered better practice currently because of expensive http requests. One of HTTP/2.0 goals is to make it cheap by multiplexing requests within same tcp connection. Server push in HTTP/2.0 can leverage it even more by sending some essential assets to the client ahead of time.

Chrome, FF, Opera and IE11 already support HTTP/2.0, and its support is available for popular webservers (apache, nginx)



来源:https://stackoverflow.com/questions/25138666/minimizing-http-connections-vs-parallel-downloads

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