<p>What are some standard practices for managing a medium-large JavaScript application? My concerns are both speed for browser download and ease and maintainability of development.</p>

<p>Our JavaScript code is roughly "namespaced" as:</p>

<pre><code>var Client = {
   var1: '',
   var2: '',

   accounts: {
      /* 100's of functions and variables */
   },

   orders: {
      /* 100's of functions and variables and subsections */
   }

   /* etc, etc  for a couple hundred kb */
}
</code></pre>

<p>At the moment, we have one (unpacked, unstripped, highly readable) JavaScript file to handle all the business logic on the web application. In addition, there is jQuery and several jQuery extensions. The problem we face is that it takes <em>forever</em> to find anything in the JavaScript code and the browser still has a dozen files to download.</p>

<p>Is it common to have a handful of "source" JavaScript files that gets "compiled" into one final, compressed JavaScript file? Any other handy hints or best practices?</p>

[{'answer_id': 15402, 'author': 'Steve M', 'author_id': 1693, 'author_profile': 'https://Stackoverflow.com/users/1693', 'pm_score': 5, 'selected': True, 'text': '<p>The approach that I\'ve found works for me is having seperate JS files for each class (just as you would in Java, C# and others). Alternatively you can group your JS into application functional areas if that\'s easier for you to navigate.</p>\n\n<p>If you put all your JS files into one directory, you can have your server-side environment (PHP for instance) loop through each file in that directory and output a <code>&lt;script src=\'/path/to/js/$file.js\' type=\'text/javascript\'&gt;</code> in some header file that is included by all your UI pages. You\'ll find this auto-loading especially handy if you\'re regularly creating and removing JS files.</p>\n\n<p>When deploying to production, you should have a script that combines them all into one JS file and "minifies" it to keep the size down.</p>\n'}, {'answer_id': 15411, 'author': 'Dan Herbert', 'author_id': 392, 'author_profile': 'https://Stackoverflow.com/users/392', 'pm_score': 2, 'selected': False, 'text': "<p>For server efficiency's sake, it is best to combine all of your javascript into one minified file.</p>\n\n<p>Determine the order in which code is required and then place the minified code in the order it is required in a single file.</p>\n\n<p>The key is to reduce the number of requests required to load your page, which is why you should have all javascript in a single file for production.</p>\n\n<p>I'd recommend keeping files split up for development and then create a build script to combine/compile everything.</p>\n\n<p>Also, as a good rule of thumb, make sure you include your JavaScript toward the end of your page. If JavaScript is included in the header (or anywhere early in the page), it will stop all other requests from being made until it is loaded, even if pipelining is turned on. If it is at the end of the page, you won't have this problem.</p>\n"}, {'answer_id': 15440, 'author': 'Mo.', 'author_id': 1870, 'author_profile': 'https://Stackoverflow.com/users/1870', 'pm_score': 2, 'selected': False, 'text': '<p>Just a sidenode - Steve already pointed out, you should really "minify" your JS files. In JS, whitespaces actually matter. If you have thousand lines of JS and you strip only the unrequired newlines you have already saved about 1K. I think you get the point.</p>\n\n<p>There are tools, for this job. And you should never modify the "minified"/stripped/obfuscated JS by hand! Never!</p>\n'}, {'answer_id': 16968, 'author': 'Kieron', 'author_id': 588, 'author_profile': 'https://Stackoverflow.com/users/588', 'pm_score': 2, 'selected': False, 'text': "<p>In our big javascript applications, we write all our code in small separate files - one file per 'class' or functional group, using a kind-of-like-Java namespacing/directory structure. We then have:</p>\n\n<ul>\n<li>A compile-time step that takes all our code and minifies it (using a variant of JSMin) to reduce download size</li>\n<li>A compile-time step that takes the classes that are always or almost always needed and concatenates them into a large bundle to reduce round trips to the server</li>\n<li>A 'classloader' that loads the remaining classes at runtime on demand.</li>\n</ul>\n"}, {'answer_id': 25322, 'author': 'gregh', 'author_id': 2687, 'author_profile': 'https://Stackoverflow.com/users/2687', 'pm_score': 1, 'selected': False, 'text': "<p>Read the code of other (good) javascript apps and see how they handle things.  But I start out with a file per class.  But once its ready for production, I would combine the files into one large file and minify.</p>\n\n<p>The only reason, I would not combine the files, is if I didn't need all the files on all the pages.</p>\n"}, {'answer_id': 39011, 'author': 'paulgreg', 'author_id': 3122, 'author_profile': 'https://Stackoverflow.com/users/3122', 'pm_score': 3, 'selected': False, 'text': '<p>Also, I suggest you to use Google\'s <a href="http://code.google.com/apis/ajaxlibs/documentation/" rel="noreferrer">AJAX Libraries API</a> in order to load external libraries. </p>\n\n<p>It\'s a Google developer tool which bundle majors JavaScript libraries and make it easier to deploy, upgrade and make them lighter by always using compressed versions.</p>\n\n<p>Also, it make your project simpler and lighter because you don\'t need to download, copy and maintain theses libraries files in your project.</p>\n\n<p>Use it this way :</p>\n\n<pre><code>google.load("jquery", "1.2.3");\ngoogle.load("jqueryui", "1.5.2");\ngoogle.load("prototype", "1.6");\ngoogle.load("scriptaculous", "1.8.1");\ngoogle.load("mootools", "1.11");\ngoogle.load("dojo", "1.1.1");\n</code></pre>\n'}, {'answer_id': 17899149, 'author': 'martyglaubitz', 'author_id': 657341, 'author_profile': 'https://Stackoverflow.com/users/657341', 'pm_score': 1, 'selected': False, 'text': '<p>My strategy consist of 2 major techniques: AMD modules (to avoid dozens of script tags) and the Module pattern (to avoid tightly coupling of the parts of your application)</p>\n\n<p>AMD Modules: very straight forward, see here: <a href="http://requirejs.org/docs/api.html" rel="nofollow">http://requirejs.org/docs/api.html</a> also it\'s able to package all the parts of your app into one minified JS file: <a href="http://requirejs.org/docs/optimization.html" rel="nofollow">http://requirejs.org/docs/optimization.html</a></p>\n\n<p>Module Pattern: i used this Library: <a href="https://github.com/flosse/scaleApp" rel="nofollow">https://github.com/flosse/scaleApp</a> you asking now what is this ? more infos here: <a href="http://www.youtube.com/watch?v=7BGvy-S-Iag" rel="nofollow">http://www.youtube.com/watch?v=7BGvy-S-Iag</a></p>\n'}]
['https://Stackoverflow.com/questions/15390', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1848/']