Vicky
Vicky

Reputation: 9575

JavaScript code compression

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.

Upvotes: 5

Views: 588

Answers (7)

Bruce
Bruce

Reputation: 8430

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.

Upvotes: 2

user258266
user258266

Reputation:

Use JSMIn its the best.

Upvotes: 0

alvincrespo
alvincrespo

Reputation: 9334

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.

Upvotes: 0

Julien
Julien

Reputation: 9442

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).

Upvotes: 1

Craig
Craig

Reputation: 36856

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.

Upvotes: 0

rahul
rahul

Reputation: 187110

You can use a javascript minifier.

YUI Compressor

JS Minifier

jsCompress

Upvotes: 6

Related Questions