tags:

views:

418

answers:

5

I'm making an AIR application (so download time doesn't have a huge impact), does combining and minifing all the javascript files affect the performance? How would obfuscating affect the performance.

+1  A: 

Minifying strips out all comments, superfluous white space and shortens variable names. It thus reduces download time for your JavaScript files as they are (usually) a lot smaller in filesize. So, yes it does improve performance.

The obfuscation shouldn't adversely affect performance.

Here's an article on the YDN that talks about minifying.

seth
+9  A: 

Minifying improves performance for your page overall by decreasing the load time (even if only slightly).

Neither minifying nor obfuscating alter execution time by any perceivable amount for the vast majority of javascript code out there.

I do recommend minifying for those reasons and more. Minifying multiple scripts together (like jQuery and it's plugins) can yield even greater savings.

Edit: As pointed out, on constrained devices and/or with very large codebases minifying could yield a noticeable result.

Gabriel Hurley
Sorry, down voted - Minifying means that there is less text to parse and variables take up less memory (and are quicker to compare, hash etc) so there can be a considerable improvement in both speed and memory usage - escpecially important on constrained devices.
Dipstick
On second thoughts I have reversed my down vote - I do not know enough about the particular javascript in AIR so may be wrong. But certainly with some javascript engines minifying can make a big difference on large applications.
Dipstick
You are right, though the cases where that would be true are few and far between. I've edited my answer.
Gabriel Hurley
I think we might be suffering confusion about what "minify" means - it is sometimes used just to describe taking out extra white space (as with jsmin) and comments but leaving the variable names the same and some "minifiers" (like packer) go further and shorten variable names as well. Obviously the first only affect download and initial parsing of code whereas the second type affect run-time performance as well.
Dipstick
Minified JS has no improvement in JS *runtime* if anything it causes performance reduction as the decompression methods used by some minification libraries defeat a number of optimisations made in modern JS engines.Obfuscating also impacts performance as it deliberately makes convoluted JS which differs from "normal" execution pattern. A good obfuscator can actually optimise perf, but i've seen obfuscators that as much as halve performance.In general variable and property name length is no longer a contributing factor in JS perf due to the many and varied caching methods in JS impls now.
olliej
+1  A: 

Minification

Minification does improve performance for two reasons:

  • Reduced file-size (because it removes comments and unnecessary white-spaces), so your script loads faster. Even if it is embedded into the <head>.

  • It is parsed faster, since comments and white-spaces don't have to explicitly ignored (since they're not there).

Combining

I've written quite a few HTML/JS AIR apps, and from personal experience, combining files won't make a difference. In fact, it's good practice to separate script based on certain criteria (classes, global functions, SQL functions etc). Helps keep them organised when the project becomes too big.

Obfuscation

Obfuscating is usually a combination of minification and renaming variables. It involves using eval to blow up the code again. This reduces performance for obvious reasons, but it depends on the size of your code.

I'd suggest running tests to understand this best for your specific situation.

[Edited to include special consideration for AIR apps]

aditya
A: 

Minification does not improve the speed at which JavaScript executes at runtime, which I believe it what you're really interested in. In fact, if you use Packer's Base64 compression, it's actually slower on initial load.

Minification will reduce the size of the JavaScript though, making your application's download size smaller.

Dave Ward
A: 

Everyone here already talked about minifying but nobody talked about the 2nd part of your question - combining. This will definitely improve performance, probably even more than minifying.

Multiple files require multiple HTTP requests, so when you put them all into one file, only 1 request is needed. This is important for 2 reasons:

  • each individual HTTP request may take longer to load for various routing reasons, and 1 file will potentially delay your whole application.
  • browsers and other clients have a max limit of files they are allowed to download concurrently from a single domain. Depending on the number of files in your application, this may mean the client queuing them up, thus making the load even longer.

Also, besides minifying and combining, you have to absolutely make sure you have some sort of server side compression enabled. This can save you 90% or even more in the amount of bytes transferred, depending on the files.

You can read more about compression (gzip, deflate) here: http://beerpla.net/2009/06/09/how-to-make-your-site-lightning-fast-by-compressing-deflategzip-your-html-javascript-css-xml-etc-in-apache/.

Artem Russakovskii
He's pointed out that it's an AIR app, hence there aren't any HTTP connections. In AIR apps, it's better to classify and separate script so that they're more organised. The more the better actually.
aditya
Ah, I assumed his AIR app communicates with a remote server, which hosts the files.
Artem Russakovskii