PDA

View Full Version : Compressed JS files



guvenck
Mar 1st, 2007, 06:40 PM
Hello,

On the web I see that some developers offer compressed (packed) versions of their JS files.

Compressing a JS file will cause less bandwidth and faster loading pages.

Which compressor is used to compress these files?

Is this method compatible with every browser?

Thanks,
Leon

felgall
Mar 1st, 2007, 07:04 PM
You can write your own compressor using Javascript or any other language or use any of the hundreds of such compressors that other people have written. Depending on how it is written the compressor will either just remove the whitespace or will do that and rename some of the variables. Depending on how well it is written it either will or wont work for all Javascript. A lot of them have problems with regular expressions, unicode, and leaving off optional semi-colons.

guvenck
Mar 1st, 2007, 08:26 PM
Thanks for the detailed answer. As you may already know, some Forum scripts support .gz compression and afaik this is supported by major browsers. Is there an chance to .gz compress a JS file, rename it to .js and run it?

eak
Mar 2nd, 2007, 06:58 PM
you can put JS into JAR files.

http://www.mozilla.org/projects/security/components/signed-scripts.html

its not widely used though.

liorean
Mar 3rd, 2007, 01:02 PM
Thanks for the detailed answer. As you may already know, some Forum scripts support .gz compression and afaik this is supported by major browsers. Is there an chance to .gz compress a JS file, rename it to .js and run it?Servers can send documents with various types of compression. gzip and deflate are common. Usually this is done by having both the uncompressed and the compressed version in the same directory and letting the server send the compressed version to clients reporting that they support that compression. Some servers can be made generate compressed files the first time they are requested and sending the compressed file after that, as long as the original remains unchanged. Servers can also be made to compress files on the fly (practical if the file contains code generated on each request), but this is considerably more costly when it comes to server CPU cycles, and will add a slight lag if the server has high load, as many virtual hosting solutions have.


Note that this compression takes place on the HTTP level. You don't compress a file and send it as were it uncompressed, you compress a file and serve it with HTTP headers telling the client what compression is being used.

MikeFoster
Mar 4th, 2007, 04:47 AM
Great info from everyone! :thumbsup:

outze
Aug 19th, 2008, 09:10 AM
Just wanted to follow up on this. In addition to server side compression its also possible to compress/obfuscate your javascript code, I think that is what you mention. I usually use Dean Edwards packer at javascriptcompressor.com (http://javascriptcompressor.com) to achieve good compression in addition to server side zipping.

A1ien51
Aug 20th, 2008, 02:35 AM
Just wanted to follow up on this. In addition to server side compression its also possible to compress/obfuscate your javascript code, I think that is what you mention. I usually use Dean Edwards packer at javascriptcompressor.com (http://javascriptcompressor.com) to achieve good compression in addition to server side zipping.

Nothing like digging up a post a 1.5 years later....lol