johnnietheblack
johnnietheblack

Reputation: 13340

How few lines can javascript be on?

I am writing a little custom minifier for javascript, and I am wondering if I need to force line breaks after a certain number of characters on a single line, or if it matters at all?

For example, I can minify an entire script into a single line with thousands of characters, or every n characters, I could insert a line break (at an acceptable point in the js code) to drop it to the next line.

Is there a reason to do this, or is does javascript/browsers not care how many characters are on a line?

Upvotes: 2

Views: 139

Answers (3)

Stephen Chung
Stephen Chung

Reputation: 14605

There are some pitfalls regarding to long lines of JavaScript. The following is from the Closure Compiler's FAQ (this link):

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.

Upvotes: 2

Bobby Jack
Bobby Jack

Reputation: 16049

I wouldn't want to bet my site on various web servers, proxies, Internet Explorer, etc. all not-choking on huge line lengths. Of course, I don't know quite how I'd define huge, but jQuery - as an example - looks like it limits to about 512 characters per line. Put it this way, I'd prefer your minifier if it allowed me to choose whether I have line breaks every x characters or not.

Upvotes: 2

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324750

You can put an entire script on one line, it makes no difference to the parser.

However breaking content onto lines of about 4kB may be beneficial to the downloader, slightly improving performance.

Upvotes: 1

Related Questions