开发者

What's the advantage of using a script loader over concatenating your script files in a build process?

开发者 https://www.devze.com 2023-03-24 22:32 出处:网络
I\'ve been hearing a lot about script loaders such as开发者_如何学编程 RequireJS. What\'s the advantage of using a script loader over concatenating your source files in a build process?Concatenating y

I've been hearing a lot about script loaders such as开发者_如何学编程 RequireJS. What's the advantage of using a script loader over concatenating your source files in a build process?


Concatenating your source files in the build process has the advantage that you the client has to make fewer requests and the request overhead is reduced and page load time decreases making it faster eg. suppose you have 10 javascript files now the browser has to make 10 HTTP requests and each request has its own overhead(request and response headers) now if you concatenate those 10 files to 2 or 3 there are just 2 or 3 HTTP requests thus reducing the overhead. also any client which follows HTTP 1.1 specification does not allow more than 2 simultaneous connections to a single domain so you see why its important to have less connections(requests)

whereas script loaders are used to load javascript on-demand,it means that suppose you are only going to need some javascript if the user types in a particular text box then you dont request that javascript on page load but only load it when it is in demand(user types in the text box). eg when you tag photos on facebook the javascipt needed for tagging is not available when the photos page loads but its requested when you try to tag the photos


Script loaders are useful to avoid having to load large volumes of JavaScript files in situations where you might not need all of it: say a page is only using 2 out of the 50 modules available, then you can use a script loader to only get the pieces you care about rather than fetching all 50. This speeds your page load time and reduces your bandwidth costs.

0

精彩评论

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

关注公众号