Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we have newlines in minified JavaScript?

I know about a similar question but it is a tiny bit off from what I am asking here, so please don't flag it as a duplicate.

When you see the production version of jQuery, why is there a newline after a while? I downloaded a copy and deleted all the newlines (apart from the licence) and it still worked. (I ran the entire unit test suite against my changes on Mozilla Firefox, Google Chrome and Opera.)

The problem. In Google Chrome's "view source"

I know three newlines (not counting the license) is not going to slow it down a lot, but still, doesn't every tiny bit help?

I have assigned myself a small challenge, to squeeze every little bit of performance out of my JavaScript code.

like image 771
Anish Gupta Avatar asked Apr 17 '12 15:04

Anish Gupta


People also ask

Why do we need to minify code?

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.

Why should you minify your JavaScript scripts before publishing them?

The sole benefit of minified JavaScript code is allowing a client to download fewer bytes, enabling the page to load faster, use less battery, use less of a mobile data plan, etc. This is usually be done as a build step when releasing code to a web server. Many tools, like uglify for example, exist to do this for you.

Should you minify your JS?

Despite being an excellent programming language, JavaScript impacts web pages by slowing them down. To regain space and improve your page load speed, you must minify the JavaScript code. The minified version of JavaScript code can reduce the file size by as much as 30–90%.

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.


2 Answers

jQuery currently use UglifyJS to minify their source code. In their build script, they specifically set the max_line_length directive to be 32 * 1024:

The documentation for UglifyJS has this to say on the max-line-len directive;

--max-line-len (default 32K characters) — add a newline after around 32K characters. I’ve seen both FF and Chrome croak when all the code was on a single line of around 670K. Pass –max-line-len 0 to disable this safety feature.

like image 157
Matt Avatar answered Oct 18 '22 09:10

Matt


To cite the Closure Compiler FAQ:

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.

This is relevant to any minification programs in general.

like image 24
katspaugh Avatar answered Oct 18 '22 08:10

katspaugh