How to Compress Javascript
Test the original code., Run the original code through a static analyzer., Minify the original code., Keep the original code., Test the minified code.
Step-by-Step Guide
-
Step 1: Test the original code.
And test it again.
It is hard to debug JavaScript errors after the code is minified.
The JavaScript becomes less recognizable due to removing whitespace and shortening variable names (just a few of the things minifying will do to your source code). -
Step 2: Run the original code through a static analyzer.
This may help identify problems before your code is compressed and/or minified by pointing out common errors.
There are some online analyzers like JSLint or JSHint. , The best thing is to try a variety of services and programs to see what works and what gives you the best compression ratio.
Some of these tools are online such as JSCompress, Dean Edward's packer, Google Closure Compiler, or the YUI Compressor. , After you minify the source code you will want to put it into your project.
Remember to keep the original code so you can make changes if (and when) a bug is found. , If you don't test your code, you're going to have a bad time.
This is because minified code is significantly different from your original code.
It is important to double-check that the functionality is exactly the same. -
Step 3: Minify the original code.
-
Step 4: Keep the original code.
-
Step 5: Test the minified code.
Detailed Guide
And test it again.
It is hard to debug JavaScript errors after the code is minified.
The JavaScript becomes less recognizable due to removing whitespace and shortening variable names (just a few of the things minifying will do to your source code).
This may help identify problems before your code is compressed and/or minified by pointing out common errors.
There are some online analyzers like JSLint or JSHint. , The best thing is to try a variety of services and programs to see what works and what gives you the best compression ratio.
Some of these tools are online such as JSCompress, Dean Edward's packer, Google Closure Compiler, or the YUI Compressor. , After you minify the source code you will want to put it into your project.
Remember to keep the original code so you can make changes if (and when) a bug is found. , If you don't test your code, you're going to have a bad time.
This is because minified code is significantly different from your original code.
It is important to double-check that the functionality is exactly the same.
About the Author
Gloria Kennedy
Experienced content creator specializing in lifestyle guides and tutorials.
Rate This Guide
How helpful was this guide? Click to rate: