5

Is there is way to compress JavaScript code?

e.g.

function test(){
  // some code here 
}

after compression it should be

function test(){//some code here} 

Also, I need vise versa at the time of editing the code.

Crescent Fresh
  • 115,249
  • 25
  • 154
  • 140
Vicky
  • 9,515
  • 16
  • 71
  • 88

7 Answers7

6

You can use a javascript minifier.

YUI Compressor

JS Minifier

jsCompress

rahul
  • 184,426
  • 49
  • 232
  • 263
2

There are a number of tools available that can reduce the download size of your javascript, improving first-load performance. The general technique of making syntactic changes to your javascript, without changing its structure, is called minification; and the tools are minifiers. I know Google has an excellent tool, as does Yahoo - there are probably others as well. Check the other responses here for links.

For more resources, try this search:

http://www.bing.com/search?q=javascript+minify

Some other things to keep in mind when optimizing your javascript:

You'll want an option to download non-minified javascript, at least on your test site - debugging minified javascript is a major pain.

Configure your web server to also compress (gzip) your javascript if the client includes the appropriate 'accept' header in their request.

Make sure you configure our cache settings for your javascript so that browsers can use their locally cached version without even sending a server request, if the file is already previously downloaded.

Bruce
  • 8,202
  • 6
  • 37
  • 49
  • Well half deserved I guess : suggesting bing is quite lame :p, plus putting a link to a search engine is not quite an answer. By the way maybe our mate didn't the "minify" word, and it could help him to find the answer, i voted up to cancel the downvote :) – Julien Jan 29 '10 at 07:30
  • I work for Microsoft, so I claim a lameness dispensation for using Bing instead of Google. *grin* I do admit that simply posting the link is a bit of snark - perhaps not quite as bad as "let me google that for you" though. I'll add some more text to my answer to atone for my sins. – Bruce Jan 30 '10 at 03:47
1

Good answers, for jquery you have a compressed version, remove the comments in the header to save some octets. For your own files, use the YUI compressor, i think it's the best.

I would add if you want to save some time, you can also put all your Javascripts files in one, so you will save some precious time with http request (only for production though).

Julien
  • 9,312
  • 10
  • 63
  • 86
0

There is already a compressed version of jQuery for you to use. For js you write yourself any of the other tools mentioned will work, I use YUI myself.

Craig
  • 36,306
  • 34
  • 114
  • 197
0

A good way to optimize your site is to include one javascript file for all. An article that explains the process of Javascript Bootstrapping can be found here.

Once you use the available compressors above, you should implement this so that your site run quicker.Hopefully this will help.

alvincrespo
  • 9,224
  • 13
  • 46
  • 60
0

Use JSMIn its the best.