Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practices for managing and deploying large JavaScript apps

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.

Our JavaScript code is roughly "namespaced" as:

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 */
}

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 forever to find anything in the JavaScript code and the browser still has a dozen files to download.

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?

like image 662
Daniel Fone Avatar asked Aug 18 '08 23:08

Daniel Fone


People also ask

How big is too big for a JS file?

URLs contain JavaScript files of size over 25 KB. The figure of 25KB is relatively arbitrary – there is no hard and fast rule as to what constitutes an JavaScript file that is 'too large'.

Where should I store JavaScript files?

JavaScript in body or head: Scripts can be placed inside the body or the head section of an HTML page or inside both head and body. JavaScript in head: A JavaScript function is placed inside the head section of an HTML page and the function is invoked when a button is clicked.


3 Answers

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.

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 <script src='/path/to/js/$file.js' type='text/javascript'> 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.

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.

like image 92
Steve M Avatar answered Oct 23 '22 17:10

Steve M


Also, I suggest you to use Google's AJAX Libraries API in order to load external libraries.

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.

Also, it make your project simpler and lighter because you don't need to download, copy and maintain theses libraries files in your project.

Use it this way :

google.load("jquery", "1.2.3");
google.load("jqueryui", "1.5.2");
google.load("prototype", "1.6");
google.load("scriptaculous", "1.8.1");
google.load("mootools", "1.11");
google.load("dojo", "1.1.1");
like image 40
paulgreg Avatar answered Oct 23 '22 17:10

paulgreg


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.

There are tools, for this job. And you should never modify the "minified"/stripped/obfuscated JS by hand! Never!

like image 4
Mo. Avatar answered Oct 23 '22 18:10

Mo.