Closure Tools

Frequently Asked Questions

What is the Closure Compiler? Why should I use it?
The Closure Compiler is a tool for making JavaScript download and run faster. You can use the Closure Compiler to reduce the size of your JavaScript files and to make them more efficient.
How is the Closure Compiler different from the other JavaScript compressors that are out there?

Generally, the Closure Compiler should meet or exceed the compression of other code minification tools, and improve download times for your web application. In addition, the Closure Compiler can help you find syntax errors during development (rather than during testing) and identify potentially buggy code patterns.

In Simple mode, Closure Compiler should be able to do better than other tools because it uses compiler-like analysis to find additional ways to minimize code size. For example, the Closure Compiler can inline functions that are used in only a few occurrences, reuse variable names, and pre-compute constant expressions.

In Advanced mode, Closure Compiler can also use your added type annotations to find hard-to-spot bugs.

Can the Closure Compiler compile JavaScript that's embedded in HTML?
No. The Closure Compiler works only on files that contain only JavaScript.
Can I use the Closure Compiler together with other JavaScript minifiers?

Yes. Closure Compiler reads in any valid JavaScript and generates valid JavaScript, so you can apply the Closure Compiler to a JavaScript file either before or after you run the file through a different minifier.

Remember that Closure Compiler and other minifiers might have expectations about the incoming code. A minifier that strips comments may remove licenses or annotation information needed by another tool, for example.

Can I use the Closure Compiler with Caja?
The Closure Compiler team and the Caja team are working on ensuring that the Closure Compiler's Simple mode preserves both the semantics and the safety of Caja's output. However, we need to do more testing before we can say with confidence that this goal has been achieved. If you find input for which Simple mode fails to preserve Caja's semantics or safety, please file a bug against the Closure Compiler. If you're interested in helping out with the validation work, contact either the Caja team (through the google-caja-discuss group) or the Closure Compiler team (through the closure-compiler-discuss group).
Does the compiler make any trade-off between my application's execution speed and download code size?
Yes. Any optimizing compiler makes trade-offs. Some size optimizations do introduce small speed overheads. However, the Closure Compiler's developers have been careful not to introduce significant additional runtime. Some of the compiler's optimizations even decrease runtime (see next question).
Does the compiler optimize for speed?
In most cases smaller code is faster code, since download time is usually the most important speed factor in web applications. Optimizations that reduce redundancies speed up the run time of code as well.
Are there any restrictions on the size of the files that can be compiled?
The compilation web service has a maximum file size, but the standalone compiler application does not.
Is the Closure Compiler avalable for all platforms?
The compiler is written in Java, so it can run anywhere Java runs.
Can the Compiler process any legal JavaScript?
Mostly. Some JavaScript constructs, including eval() and with(), can invalidate assumptions on which the compiler's transformations are based.
How much do I need to know about web development to use the Closure Compiler?
The Closure Compiler is a tool for JavaScript development, so you do need to know how to program in JavaScript to use the compiler. But anyone who uses JavaScript can benefit from using the Closure Compiler.
How does the Closure Compiler work with the Closure Library?
The Closure Compiler provides special checks and optimizations for code that uses the Closure Library. In addition, the Closure Compiler service can automatically include Closure Library files. Finding Your Way around Closure describes the syntax for declaring the parts of Closure that you need. See the API reference for information on using the Closure Library with the API. To use the Closure Library with the Closure Compiler application you must first download the Closure Library. Support for the Closure Library is enabled in the compiler application by default.
My code stops working or the Compiler produces errors when I compile with ADVANCED_OPTIMIZATIONS. Why?
Using Advanced mode usually requires some preparation and code changes. Advanced Compilation and Externs explains how to make sure your code works with ADVANCED_OPTIMIZATIONS.
Why are there random line feeds in compiled scripts?
The Closure Compiler intentionally adds line breaks every 500 characters or so. Firewalls and proxies sometimes corrupt or ignore large JavaScript files with very long lines. Adding line breaks every 500 characters prevents this problem. Removing the line breaks has no effect on a script's semantics. The impact on code size is small, and the Compiler optimizes line break placement so that the code size penalty is even smaller when files are gzipped.
I have copyright notices or open source license text that must appear in my source code. How do I keep the Closure Compiler from stripping this text out?
Closure Compiler supports the JSDoc @license tag. Add the @license tag to any JSDoc comment to preserve the comment in the compiler output. See Annotating JavaScript for the Closure Compiler for more information.

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.