8 JavaScript optimizations Closure Compiler can do conventional minifiers just can’t

Closure Compiler is a JavaScript type checker and optimizer. But just what can these optimizations do to shrink that oh-so-important bundle size?

Disclaimer: This is not a completely fair test. Please don’t flame me.

  • Smaller, simpler programs tend be easier to statically analyze and can be optimized more aggressively
  • The ES6 polyfils from Babel tend to be quite heavy compared to Closure which are a one time cost and look bad in small examples
  • Closure and Babel/Uglify do not make the same assumptions. Closure optimizations break code that abuses the dynamic nature of JS. See full restrictions
  • I am not a Babel/Uglify expert. Please alert me if I am using these tools incorrectly or ineffectively

1. Inline Functions

playground link

Closure: 17b Babel-minify: 34b

2. Strip Dead Code

playground link

Closure: 17b Babel-minify: 339b (ES6 polyfils are heavy here)

3. Rename Prototype properties

playground link

Closure: 56b Babel-minify: 216b (ES6 polyfils are heavy here)

4. Inline Variables

playground link

Closure 25b Babel-minify: 78b

5. Statically infer call graph

playground link

Closure: 34b Babel-minify: 727b

6. Determine function side effects for accurate code pruning

playground link

Closure: 108b Babel-minify: 311b

7. Collapse object namespaces / static functions

playground link

Closure: 257b Babel-minify: 798b

8. Remove unused returns

playground link

Closure: 86b Babel-minify: 237b