Hacker News new | ask | show | jobs
by cromwellian 3349 days ago
There are too many optimizations in Closure Compiler to describe simply what it does. It is a full blown optimizing compiler like C, right down to supporting graph coloring in the naming of locals. It even does type-based optimization by disambiguating property names on prototypes by types (https://github.com/google/closure-compiler/wiki/Type-Based-P...) -- this alone I've measured at 14% code size reduction in my projects, and it can move code at the function level out of the main module into late loaded modules if it detects the code is not used until later.

Malte Ubl has exposed some of this functionality to the outside world with his Splittable project (https://medium.com/@cramforce/introducing-splittable-1c882ba...)

All of Google's main projects, from the Google Home page, to Docs, Gmail, Maps, etc rely on Closure Compiler. Google Photos makes particularly aggressive use of this splitting functionality (take a look at the network tab in Chrome)

Closure Compiler also pairs well with Google Style Sheets, which is a style sheet compiler like SASS/LESS (but predated them), but it optimizes and prunes CSS.

Closure has it's downsides, the Advanced Mode makes assumptions about the way you write Javascript that can break. However, there are conformance checks you can enable to check for many of these. The compiler has to "see" what you're doing, so if you do something like this:

foo.hello = 42;

function blah() { return "hello"; }

foo[blah()] = 42;

The compiler may not be able to see that 'foo.hello' and 'foo["hello"]' are the same thing, and the former will get renamed to foo.xyz. The price you pay huge savings in code size is being consistent and diligent in your code, and sometimes giving up a little bit of dynamism