📜 ⬆️ ⬇️

Javascript Optimization with Google Closure Compiler

image
Google opened the source codes of several tools that were used by their employees in developing projects such as Google Docs, Google Maps and GMail.

Among these tools there is a rather interesting thing called Google Closure Compiler. This utility minimizes and optimizes javascript code, due to which it starts to work and load faster.

The “compiler” can be downloaded from the link closure-compiler.googlecode.com/files/compiler-latest.zip (approximately 3 MB), but in order to watch it in action it is enough to go to closure-compiler.appspot.com .
Before you open a window that is divided into 2 parts: on the left you enter your code, and on the right you get the result. By default, there will already be a Hello World feature.
image

Click the “Compile” button to see the result.
image
As can be seen from the figure, this simple example “shrank” by 40.22%.
')
But you can try to squeeze something bigger. To do this, press the “reset” button, in the “Add a URL” field, enter “ habrahabr.ru/js/1257170514/tm/validation.js?revision=29 ” and press the “Add” and “Compile” buttons image

This code is “shrunk” by 68.40%. In order to assess the degree of optimization, in the “Add a URL” field, select a known library from the drop-down list and see how it will be compressed.
image

There are already quite a lot of optimizers javascript codes, but, as stated on the project site, in most cases Closure Compile optimizes javascript better.

Google also opened the source code for two more interesting projects:
Closure Library and Closure Templates

Source: https://habr.com/ru/post/74438/


All Articles