最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Why do many websites put all of their JavaScript on a single line? - Stack Overflow

programmeradmin6浏览0评论

I'm starting to learn JavaScript for my first language and I am kind of starting to get the hang of it.

But I'm beginning to check out source codes of .js files in websites and I see a lot of people put the the entire script in just one line which. For one, this is hard for me to understand from a learning point, but my main question is: is there a benefit for doing this, rather than coding it in more of a block style?

I'm starting to learn JavaScript for my first language and I am kind of starting to get the hang of it.

But I'm beginning to check out source codes of .js files in websites and I see a lot of people put the the entire script in just one line which. For one, this is hard for me to understand from a learning point, but my main question is: is there a benefit for doing this, rather than coding it in more of a block style?

Share Improve this question edited Nov 13, 2012 at 16:05 Jeremy Banks - Vive le Canada 130k88 gold badges358 silver badges381 bronze badges asked Jul 14, 2011 at 15:40 T MikT Mik 753 bronze badges 3
  • 4 Minification in a Nutshell – ChaosPandion Commented Jul 14, 2011 at 15:42
  • Look at jquery., when you download jquery you get the option of getting a minified version or development version. You will notice a huge difference in the file size. The development version is much larger but is made to be edited and read by human beings. Minified versions are meant to be read by machines only. – Caimen Commented Jul 14, 2011 at 15:45
  • 1 Tool to make make it pretty again jsbeautifier. Variables might be horrible, but it will be formatted. – epascarello Commented Jul 14, 2011 at 15:46
Add a ment  | 

8 Answers 8

Reset to default 12

The developers of the code won't be writing it like that themselves, they'll be using a normal style. Because JavaScript isn't piled, developers often run it through a tool called a minifier instead:

Minification in puter programming languages and especially JavaScript, is the process of removing all unnecessary characters from source code, without changing its functionality. [...] Minified source code is especially useful for interpreted languages deployed and transmitted on the Internet (such as JavaScript), because it reduces the amount of data that needs to be transferred.

Minified code is harder to understand; this is often seen as a feature because it makes it harder for other people to copy your code and use it themselves. Some minification tools, such as Google's Closure Compiler, also optimize the code, improving performance and removing redundant code.

You should develop using normal indented code and, if you want, run it through a minifier when publishing it on your web server.

benefit for doing this rather

It makes the script smaller, so it downloads faster.

than coding it in more of a block style

The scripts will be coded in block style. They are just minified as part of the publishing process.

For file size. The easiest way to reduce the number of bytes downloaded to a user's browser is to remove all unnecessary whitespace (including newlines) from every JavaScript and CSS file. Depending on the number of lines, that could save several kilobytes.

You're probably looking a scripts that have been "minified", that is, all of the unnecessary line breaks and white space, etc. have been removed. The advantage of this is that the script has a smaller memory footprint and uses less bandwidth when it's transmitted over the wire.

Each character == a unit of digital information (bits, bytes, etc...)
Less Bits and Bytes === faster loading times.

It's called minifying. It's not written like that originally.

Wikipedia:

Minified source code is especially useful for interpreted languages deployed and transmitted on the Internet (such as JavaScript), because it reduces the amount of data that needs to be transferred. Minified source code may also be used as a kind of obfuscation. Source

Minification, removing unnecessary white spaces and characters to reduce the size.

You'll find the script has been put in one line when it is 'minified'. This means that unnecessary white space and all ments have been removed.

A Script file is minified to reduce the size of the JavaScript file in order to optimise it for download from the server to the client browser.

All the minified script will have been developed as a block of code as you describe, and then passed through a 'minifier' like JSMin

There is a further stage referred to a zipping, which is to give all variables and function the smallest possible names ('a', 'b', etc).

In addition, both these stages are useful as a crude obfuscator

You will find that this is more a deployment step than a discreet developement one. The original version of the script will (or at least should) be retained by the owner.

They don't code in one single line, once the code is ready, they remove spaces and ments so the code is not too heavy and will load quickly

发布评论

评论列表(0)

  1. 暂无评论