开发者

What's better? More HTTP requests = less data transfered or Less HTTP requests = more data transferred?

开发者 https://www.devze.com 2023-03-15 19:18 出处:网络
Sites like Facebook use \"lazy\" loading of js. When you would have to take in consideration开发者_运维技巧 that I have one server, with big traffic.

Sites like Facebook use "lazy" loading of js. When you would have to take in consideration开发者_运维技巧 that I have one server, with big traffic.

I'm interested - which one is better?

When I do more HTTP requests at once - slower loading of page (due to limit (2 requests at once))

When I do one HTTP request with all codes - traffic (GB) is going high, and apaches are resting little bit more. But, we'll have slower loading of page.

What's faster in result ?


Less requests! Its the reason why we combine JS files, CSS files, use image sprites, etc. You see the problem of web is not that of speed or processing by server or the browser. The biggest bottleneck is latency! You should look up for Steve Souders talks.


It really depends on the situation, device, audience, and internet connection.

Mobile devices for example need as little HTTP requests as possible as they are on slower connections and every round trip takes longer. You should go as far as inline (base-64) images inside of the CSS files.

Generally, I compress main platform and js libs + css into one file each which are cached on a CDN. JavaScript or CSS functionality that are only on one page I'll either inline or include in it's own file. JS functionality that isn't important right away I'll move to the bottom of the page. For all files, I set a far HTTP expires header so it's in the browser cache forever (or until I update them or it gets bumped out when the cache fills).

Additionally, to get around download limits you can have CNAMES like images.yourcdn.com and scripts.yourcdn.com so that the user can download more files in parallel. Even if you don't use a CDN you should host your static media on a separate hostname (can point to the same box) so that the user isn't sending cookies when it doesn't need to. This sounds like overfill but cookies can easily add an extra 4-8kb to every request.

In a developer environment, you should be working with all uncompressed and individual files, no need to move every plugin to one script for example - that's hard to maintain when there are updates. You should have a script to merge files before testing and deployment. This sounds like a lot of work but its something you do for one project and can reuse for all future projects.

TL;DR: It depends, but generally a mixture of both is appropriate. 'Cept for mobile, less HTTP is better.


The problem is a bit more nuanced then that.

If you put your script tags anywhere but at the bottom of the page, you are going to slow down page rendering, since the browser isn't able to much when it hits a script tag, other then download it and execute it. So if the script tag is in the header, that will happen before anything else, which leads to users sitting there stairing at a white screen until everything downloads.

The "right" way is to put everything at the bottom. That way, the page renders as assets are downloaded, and the last step is to apply behavior.

But what happens if you have a ton of javascript? (in facebooks example, about a meg) What you get is the page renders, and is completely unusable until the js comes down.

At that point, you need to look at what you have and start splitting it between vital and non vital js. That way you can take a multi-stage approach, bringing in the stuff that is nessicary for the page to function at a bare minimum level quickly, and then loading the less essential stuff afterwards, or even on demand.

Generally, you will know when you get there, at that point you need to look at more advanced techniques like script loaders. Before that, the answer is always "less http requests".

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号