Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ways to compress/minify javascript files [duplicate]

Duplicate:

  • Best javascript compressor
  • Which javascript minification library produces better results?
  • What is the best method to reduce the size of my Javascript and CSS files?

So far I have seen these tools to compress javascript files

  • Packer
  • JSMin
  • Yahoo Compressor

On Packer page there is a section "Packer versus JSMin" which says that JSMin can be more efficient than Packer. On Yahoo Compressor page it states that Yahoo compressor is more efficient than JSMin, so it looks like Yahoo compressor might be a best candidate.

What is the best option for compressing javascript files?

like image 780
dev.e.loper Avatar asked May 19 '09 14:05

dev.e.loper


People also ask

How do I reduce the size of a JavaScript file?

Minification can help reduce JavaScript file sizes by removing comments, white spaces, and redundant code, and in some cases, also makes the code more efficient by using shorter variable and function names. The smaller these JavaScript files are, the faster they are downloaded, parsed, and executed by the browser.

How do I un minify a JavaScript file?

You can't unminify a minified file. Minification is a desctructive operation and involves loss of information. For example, if you have a function with a descriptive name, after minifcation that name will be substitute with a meaningless one. There's no way to go back from this operation.

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.

How do I compress JavaScript and CSS files?

Go to minifycode.com and click the CSS minifier tab. Then paste the CSS code into the input box and click the Minify CSS button. After the new minified code is generated, copy the code. Then go back to the css file of your website and replace the code with the new minified version.


2 Answers

Yahoo's compressor, for a combination of safety, and decent compression.

It uses a real JavaScript parser, rather than using a set of regular expressions. It is able to do the same kind of variable renaming that Packer can, but is much safer. It won't break if you forget to put a semicolon at the end of a function definition.

Packer does have an encoding feature that generates an even smaller file. However, it's only effective if your web server does not support gzip or deflate compression. With gzip / deflate, YUI and Packer generate files that are about the same size.

like image 83
BlackAura Avatar answered Sep 18 '22 17:09

BlackAura


I use YUICompressor exclusively because it intelligently minifies removing whitespace and reducing internal variables down whilst maintaining external refs properly and has yet to break my code and it also does CSS to boot!

After that we serve up on a GZip HTTP connection and voila!

like image 45
Lloyd Avatar answered Sep 18 '22 17:09

Lloyd