Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens to comments in minified JavaScript?

What happens to comments in a minified JavaScript file? How can the browser know when the end of the comment is when everything is condensed to one line? Take this little example, I have Google tracking code like this:

//Google tracking
var _gaq = _gaq || []; 
_gaq.push(['_setAccount', '123456']); 

The minified version pulls everything into one line

// Google tracking var _gaq = _gaq || []; _gaq.push(['_setAccount', '123456']);

There are more statements, but when I examine the JavaScript code in an editor, it looks like one giant comment (more or less). Is there a hidden character that tells the browser when to end a comment or is this code just not being executed?

like image 433
Rondel Avatar asked Feb 27 '12 15:02

Rondel


People also ask

What does it mean when code is minified?

Minification is the process of minimizing code and markup in your web pages and script files. It's one of the main methods used to reduce load times and bandwidth usage on websites. Minification dramatically improves site speed and accessibility, directly translating into a better user experience.

What is minified in JavaScript?

Minification, also known as minimization, is the process of removing all unnecessary characters from JavaScript source code without altering its functionality. This includes the removal of whitespace, comments, and semicolons, along with the use of shorter variable names and functions.

What is difference between minified and Unminified JS?

Minification means removing all the unnecessary characters from the source code without changing its functionality. These characters are needed to add readability to the code, but are not required for it to execute.

Is minified JavaScript faster?

Minification does improve performance for two reasons: Reduced file-size (because it removes comments and unnecessary white spaces), so your script loads faster. Even if it is embedded into the <head> . It is parsed faster, since comments and white spaces don't have to be explicitly ignored (since they're not there).


2 Answers

Minifiers strip comments or insert line-breaks. For example, Closure Compiler's FAQ says:

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.

Sometimes you really need a comment in which case they put in a line break.

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.

Minifiers also tend to break lines occasionally, because some interpreter's source code parsers crashed or performed really slowly on really long lines.

https://bugzilla.mozilla.org/show_bug.cgi?id=634444

Previously, because we were dealing with chunks, there was a limit to how much of a line an error message could include. But now the error message contains the entire line. If you have very long lines and lots of errors, it's a recipe for high memory usage, especially since we call js_DeflateString() on the error message string, resulting in two copies of it (one made of jschars, the other made of chars).

On the site in question, heaps of errors occurred on a line containing 122,895 chars, resulting in over 1G of chars (at 3 bytes per char!) being put into error messages.

like image 126
Mike Samuel Avatar answered Oct 27 '22 15:10

Mike Samuel


JavaScript single-line comments end at the first new-line character reached. A minifier will remove comments before removing newlines so that it won't break your code.

For an example, if you paste

//GOOGLE TRACKING
var _gaq = _gaq || []; 
_gaq.push(['_setAccount', '123456']); 

into Closure Compiler with simple optimizations you get

var _gaq=_gaq||[];_gaq.push(["_setAccount","123456"]);

With advanced optimizations you get:

var a=a||[];a.push(["_setAccount","123456"]);

The comments are removed in both.

like image 32
Paul Avatar answered Oct 27 '22 16:10

Paul