2

I just started using requirejs and I love it. I have one concern though. I've been compressing all my js files into one single file. Even with requirejs optimizer, I need to load module files from the server time to time and I'm concerned with it.

Performance and user experience wise, which one is better?

  • I am not sure I understand your concerns. With the optimizer you can get away with just two files: requireJS itself and the optimized file. Is it a problem loading two files instead of one? – Andrea Jun 29 '12 at 20:18
4

Performance and user experience wise, which one is better?

A single compressed file.

It is a single connection, so the browser is free to download other assets.

It is compressed, so it takes less time to transfer to the browser.

Both mean that the page and the javascript run faster - this is better user experience and better performance.

Win win.

  • @ooded // but the problem of compressing all the files is that the result file might contain codes that users might never hit. What do you think about it? – Moon Jun 29 '12 at 19:44
  • 1
    @Moon - Yes, that's a risk. Some minification frameworks let you tailor which files go with which page, to remove this issue. – Oded Jun 29 '12 at 19:46
  • It will get browser cached though and only needs to be cache busted if some code was changed. Modern frameworks like MVC 5's Script Bundler and MVC 6's Tag Helpers handle this cache busting for you automatically. So imo, having unused code in the file isn't such a huge deal. My preferred method though is to have 3 JS files. All the libraries I use compressed into 1 js file which loads first. Then my app's JS file (jquery extensions, prototypes, and app specific global code) which loads second. And finally my page specific JS files (if the page has one). So 2 in some cases, 3 at most. – Ryan Mann Aug 22 '16 at 21:08
2

The reason to break up your .js files is to reduce the initial load time to be as small as possible. the first load is the most critical because it generally determines if a user will bother with exploring your site further or they perceive it as slow. If that first load is important to you it may be worth taking out unneeded files for your homepage and checking if it noticeably changes that initial load, otherwise Oded's advice is solid.

  • First page load is actually pretty easy to not be an issue if you load your script in the footer and don't use js base styles, js less/sass processors. E.g. you build and compile all your js and sass with node to finalized distributions. Style your site with straight css (compiled from w/e in node js) and don't do any dynamic jss style changes that your site depends on to look fully loaded. You put your 1 Css link in the header, and your JS in the footer. The site will look loaded near instantly, even though the JS might still be loading for a few hundred milliseconds. – Ryan Mann Jan 27 '17 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.