Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 410
Kevin Avatar asked Aug 04 '11 03:08

Kevin


1 Answers

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

like image 54
lovesh Avatar answered Oct 26 '22 23:10

lovesh