How to speed up the minification process of UglifyJS 2?
Problem
I am using Uglifyjs 2 to concatenate and minify a bunch of JavaScript files (not too much, around 5 to 10). This process is run from within Node.js by using the default settings of UglifyJS 2.
The files are basically Require.js, jQuery, Backbone.js, Backbone.js Marionette, Moment.js, and some additional (smaller) helper files, all in the uncompressed (i.e. development) versions.
The problem is that this process nearly takes 10 seconds.
If I disable Minification completely by handing over
{ compress: false }
as an option, it's a lot faster, but it still takes around 2 seconds.
Question #1: Is it usual that UglifyJS 2 takes that long even for a few files? Or am I most probably doing something wrong?
Question #2: How can I speed up this process, without disabling all the useful options of UglifyJS 2?
Solution
It's not uncommon that minifiers take a few seconds to compress multiple large files. 8-12 seconds is okay in your case (multiple large libraries).
However, I recommend not to compress these libraries yourself. Most of them have a team of developers behind them who really know what they're doing, I recommend downloading the minified versions from the source and do as simple as
copy *.min.js allLibraries.js
or
cp *.min.js allLibraries.js
Note: Make sure you concatenate in the correct order, this usually means The main libraries -> The plugins -> Your code
.
As for speeding up, other than disabling the cool features like the strong compression, there's nothing much you can do.
Discussion
View additional discussion.