Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Google's Closure Compiler leave a few unnecessary spaces or line breaks?

I've noticed that every time I use Google's Closure Compiler Service, it leaves a few unnecessary spaces in the compiled code presented on the right-hand side of the page. These correspond to line breaks in the hosted version of the compiled code.

For example (note the line breaks, each of which seems unnecessary):

http://troy.onespot.com/static/stack_overflow/closure_spaces.js

To date, I've just been removing them manually, but I'm curious why they're there. Is it to limit the line length of the hosted version of the code to make it more readable? Could the compiler be smart enough to leave or insert those intentionally to maximize GZIP compression efforts?

I know that they have a trivial effect on the file size, but with so much effort going into minifying every last byte in the source script, it's counterintuitive why they're there.

like image 495
Bungle Avatar asked Jun 10 '10 20:06

Bungle


People also ask

Why use Google Closure?

The Closure Compiler reduces the size of your JavaScript files and makes them more efficient, helping your application to load faster and reducing your bandwidth needs. Code checking.

How does Closure compiler work?

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.

What is the purpose of advanced mode in closure compiler?

Solution(By Examveda Team) In Advanced mode, the Closure Compiler rewrites the JavaScript by renaming variables and functions from longer descriptive names to single letters to save file size, and it inlines functions, coalescing them into single functions wherever it determines that it can.


1 Answers

Quoting the Closure Compiler FAQ:

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.

You knew it was smart! :)

like image 89
Daniel Vassallo Avatar answered Nov 10 '22 20:11

Daniel Vassallo