user623990
user623990

Reputation:

Is minifying jQuery/HTML a good idea?

Is it a good idea to minify the jQuery or even the HTML on my website? How could I do this and still have a nice structure to work with?

Upvotes: 0

Views: 1563

Answers (3)

austincheney
austincheney

Reputation: 1115

I wrote a tool to do just this. It can minify JavaScript, CSS, and HTML.

http://prettydiff.com/?m=minify&html

It is important to keep in mind some distinctions.

  • Minify - This is to reduce the file size of a document without otherwise altering the syntax or naming conventions in the code. Typically this process only involves removing comments and reducing white space.

  • Obfucation - The word literally means to make more confusing. This process involves minification, reducing variable names and labels to something with the fewest available character length, removing dead code, and other alterations.

Minification is not harmful so long as your documentation and comments are backed up. This is because the reduced code can always be restored using an algorithmic beautifier. Obfuscation is harmful because an automated process cannot restore obfuscated code. It can be significantly smaller than merely minified code, but such extraordinary gaps are not common.

A common false misconception is that obfuscation can be used as a layer of security, because the code is more difficult to read. Obfuscated code demands some amount of effort to research, but this primarily due to missing documentation and for no other reason since the code can be passed through an algorithmic beautifier. Since JavaScript is always interpreted plain text there is no security associated with the code base.

Another false misconception is that HTML cannot be minified. If the code is valid HTML then it can be minified without any harm to a recursive beautification operation. The only one exception to this rule is that certain stylesheet properties capture and print out all white space characters, such as white-space:pre, but this not a default presentation on any user agent and is not commonly used. I explain in further detail about what an acceptable level of HTML minification means in the Pretty Diff documentation.

Upvotes: 1

George Stocker
George Stocker

Reputation: 57907

Smaller footprint. Also, if you view it in Chrome, it's not on one line.

Google talks about their methodology in this document:

Minify HTML

Overview

Compacting HTML code, including any inline JavaScript and CSS contained in it, can save many bytes of data and speed up downloading, parsing, and execution time.

Details

Minifying HTML has the same benefits as those for minifying CSS and JS: reducing network latency, enhancing compression, and faster browser loading and execution. Moreover, HTML frequently contains inline JS code (in <script> tags) and inline CSS (in <style> tags), so it is useful to minify these as well.

Note: This rule is experimental and is currently focused on size reduction rather than strict HTML well-formedness. Future versions of the rule will also take into account correctness. For details on the current behavior, see the Page Speed wiki.

Tip: When you run Page Speed against a page referencing HTML files, it automatically runs the Page Speed HTML compactor (which will in turn apply JSMin and cssmin.js to any inline JavaScript and CSS) on the files and saves the minified output to a configurable directory.

Upvotes: 9

No they don't do this intentionally. They use a template engine and, as many server side template engines, they don't really care how it looks like after compilation.

Some exaples:

It is important to note, you cannot obfuscate a page. For instance, if you use Chrome it has a built in inspector. I never notice when a page is inline, uses encoding sheningans or is otherwise obfuscated.

http://www.youtube.com/watch?v=nOEw9iiopwI

Upvotes: 1

Related Questions