Many days ago, in the JavaScript article tutorials about optimization for web performance, jsB@nk had also mentioned the importance of JavaScript files compression. You can still read them again:
- Performances of JavaScript Compressors
- JavaScript Performance Tips
- Speed Up JavaScript Performance with SharePoint Minifier
- Automatically Compressing JavaScript files on Apache
Today in this post, jsB@nk would like to provide you another JavaScript tutorial for optimizing JavaScript source codes/files with Google Closure. This is very detailed JavaScript article tutorial with full instructions and illustrative pictures. Please go to the inner page for it.
- Demo
- Enlarge
- Reload
- New window
Free iPage Web Hosting for First Year NOW
If you're still looking for a reliable web host provider with affordable rates, why you don't take a little of time to try iPage, only with $1.89/month, included $500+ Free Extra Credits for the payment of 24 months ($45)?
Over 1,000,000+ existisng customers can not be wrong, definitely you're not, too! More important, when you register the web hosting at iPage through our link, we're going to be happy for resending a full refund to you. That's awesome! You should try iPage web hosting for FREE now! And contact us for anything you need to know about iPage.
One of the best practices to increase our site performance is to reduce the JavaScript file size and saving time for downloading it. On 5th of November 2009, Google has announced the release of Google Closure Compiler. So, what is that all about? In a nutshell, Closure Compiler is a tool that is used to rewrite our JS code to make it faster and more compact. In this post, I will go through with you, how can we build and compile our JS code with this optimizer on Windows platform.
To run the compiler on our machine, we need Java 6 or higher to be installed. If you have already had Java 6 or higher installed, just grab a Closure Compiler executable file. There are three ways for getting it:
1) The pre-built version - it can be found and downloaded at: http://code.google.com/p/closure-compiler/downloads/list
2) Check out the source and build it with Apache Ant
3) Check out the source and build it with Eclipse.
However, I will not explain into details for 2) and 3). I will use
the pre-built version for compiling JS file instead. So, before we run
the closure compiler, we need the executable file first. Just download
the latest copy from this link.
Once we have downloaded the compressed folder, extract all files and
locate it in an empty folder, say 'work' folder. We should be able to
see 3 files inside, as follows:
As what you have seen, the compiler is in executable jar file format. And, the 'README' is the guide of running this optimizer tool. Running the compiler is pretty straight-forward, just follow these steps will do:
1) Say, I would like to compress 'facebox' jQuery plugin which is
not compressed and the original file size is 10KB. What I want is to
reduce its file size so that my site is not taking much time for first
downloading the JS file. So, what I need to do is, copy the original JS
file to the 'work' folder, like so:
As you can see, the original file size is 10KB in the folder.
2) Next, open the Windows' command line, and change the directory to the 'work' folder, as the screenshot shown at below:
We are now at the 'work' folder. To execute the compiler, type in the following command:
3) Once we pressed 'Enter', this starts the compiler in interactive
mode. The next thing we need to do is to type in 'var x = 17 + 25;' ,
just as follows:
4) Next, hit the 'Enter' key once we are done, and then hit the
'Ctrl-Z' keys and once again, hit the 'Enter' just after the 'Ctrl-Z'.
The compiler will respond to us as 'var x=42;', as shown in this
following srceenshot:
5) OK now, basically we are able to rewrite and compile our JS file
at this stage. The optimizer tool has many options to read the input
file and checking our code and so on. Such as, how to compile multiple
JS files, etc. However, we will focus on one file in this tutorial. To
compress and reduce 'facebox' plugin file, type in and run the
following command:
To make it clearer, this command should be something like this:
java -jar compiler.jar --js facebox.js --js_output_file facebox-compiled.js
Please take note that, you are free to name the output of your JS file. In this case, I named it as 'facebox-compiled.js'.
6) Open the 'work' folder again after the command is executed, you
should be able to see an additional file (facebox-compiled.js) inside
the folder:
If you have noticed, the original file size has been reduced from 10KB to 5KB, which is 50% of reduction with the file size! To see the code, open the compressed file with your preferred text-editor, you should be able to see the code has been optimized and rewritten as well as obfuscated. This will prevent visitors to read your JS code straight from their browser's 'View Source' option. Below is the example how does the code look like:
To learn more about Closure Compiler, please visit: http://code.google.com/closure/compiler/docs/gettingstarted_app.html. Hope you enjoyed it.
Conclusion:
Google Closure Compiler is a powerful optimizer tool to analyze and
rewritten our JS code. It will greatly reduce our file size as well. JS
file size should always be concerned for the sake of the site
performance.
- Sent (0)
- New
Generate your business videos by AI with voice or just text
chatGPTaz.com
Talk to ChatGPT by your mother language
AppAIVideo
Your first FREE AI Video App
Deepfake Video
Deepfake AI Video Maker
Deepfake
Deepfake AI Video Maker
AI Deep Fake
Deepfake AI Video Maker
AIvidio
AI Video Mobile Solutions
AIvideos
AI Video Platform & Solutions
AIvedio
AI Video App Maker
Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE
Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE
Temu Free $500 for New Users
Claim Free Temu $500 Credit via Affiliate & Influencer Program
Free TikTok Ads Credit
Master TikTok Ads for Your Business Marketing
Dall-E-OpenAI.com
Generate creative images automatically with AI
chatGPT4.win
Talk to ChatGPT by your mother language
First AI Product from Elon Musk - Grok/UN.com
Speak to Grok AI Chatbot with Your Language
Tooly.win
Open tool hub for free to use by any one for every one with hundreds of tools
GateIO.gomymobi.com
Free Airdrops to Claim, Share Up to $150,000 per Project
iPhoneKer.com
Save up to 630$ when buy new iPhone 16
Buy Tesla Optimus Robot
Order Your Tesla Bot: Optimus Gen 2 Robot Today for less than $20k