Page 1 of 1

gz/bz2 library

Posted: Wed Dec 14, 2005 9:17 pm
by feyd
Would anyone know of a (small) implementation of a gzip or bzip2 decompressor (or library) written in Javascript?

We've got a crapload of Javascript that could use real compression to help crush it down to nice bite sizes.... I haven't seen any around in the bit of searching I've done.. hoping someone has a secret stash somewheres... :)

Re: gz/bz2 library

Posted: Wed Dec 14, 2005 11:47 pm
by alex.barylski
feyd wrote:Would anyone know of a (small) implementation of a gzip or bzip2 decompressor (or library) written in Javascript?

We've got a crapload of Javascript that could use real compression to help crush it down to nice bite sizes.... I haven't seen any around in the bit of searching I've done.. hoping someone has a secret stash somewheres... :)
Does it have to be gzip?

Kinda of weird request...but hey...why not right?

Anyways, this annoys me when people do this...so I'm being a hypocrite, but...

Have you tried searching for: http://www.google.ca/search?hl=en&q=jav ... sion&meta=

Just incase you didn't search these keywords... :)

Posted: Thu Dec 15, 2005 8:12 am
by feyd
well... it doesn't have to be gzip, but it does need to be a compression that php can perform very quickly (read compiled command, preferably built-in) .. of which there aren't many, none of which seem to have libraries built in Javascript (yet.)

Google was of no help that I could see, hence my posting a new thread. ;)

Thanks though..

Posted: Thu Dec 15, 2005 8:46 am
by Chris Corbyn
I'd never really thought about this... see server side gzip compression would help if the browser supported it but apparently it can cause issues with CSS/JS (not sure why :?).

I've been googling too but I don't see anything myself. It'd be interesting to see if somone could grab an implementation of gzip in another language and use it to write a JS implementation.... I'd guess that performace would be poor but you only need to run it once at page load (and it would be nice to cache if that were possible).

It's possible to really cut down on file-size by converting all vars/functions to really short, meaningless names (in production only) and removing just about all whitespace but that's by no means *real compression*.

I'll be interested to see if this has been done or if someone could implement it.

Posted: Thu Dec 15, 2005 9:06 am
by feyd
right. We do the white-space thing already, I'm considering the on-the-fly renaming, but real compression would help.

The major reason it can break CSS and Javascript is mostly due to poor creation of the code. For instance, not placing semicolons where they should be required, but due to very lax implementations and loose strict handling of code. Here's a quick example:

Code: Select all

SomeType.prototype.SomeMethod = function(a,r,g,s) {
  return a * r / g % s;
}

SomeType.prototype.SomeOtherMethod = function(a,r,g,s) {
  return a / r * g << s;
}
With white-space removed, the above Javascript will error, whereas as-is, it will not in most engines (reasons above).

Compacted:

Code: Select all

SomeType.prototype.SomeMethod=function(a,r,g,s){return a*r/g%s;}SomeType.prototype.SomeOtherMethod=function(a,r,g,s){return a/r*g<<s;}
Why does it break?
There's a semicolon missing after the function definitions:
...turn a*r/g%s;};SomeType.p...

Re: gz/bz2 library

Posted: Thu Dec 15, 2005 10:23 am
by Roja
feyd wrote:We've got a crapload of Javascript that could use real compression to help crush it down to nice bite sizes.... I haven't seen any around in the bit of searching I've done.. hoping someone has a secret stash somewheres... :)
I completely misunderstood your goal from the first sentence.

You don't need a js implementation of gzip - you need a way to gzip compress your javascript files, yes?

If so: http://www.fiftyfoureleven.com/sandbox/ ... ip-method/

Usable for javascript libraries, css, and more.

Posted: Thu Dec 15, 2005 10:47 am
by feyd
It's a bit of both, potentially.. we have to do some testing but there's a path of gzipping the code with a live decompression, then there's gzipping the page itself.. We likely need both as a browser may support gzipped content natively, or we need to transport the content normally but gzipped internally to unpack itself on the client side.

A basic page in our application can easily be 100K, with is transporting in much more via Ajax as they move around (we clean out "old" data after a while to keep the overall footprint fairly small)

There's a small amount of security-by-obscurity here too. (Not my initiative, just my task to take care of.) Which is mostly a side-effect of doing the compression inline. Sure, a "smart" person could unpack it quite easily... again, I'm not personally after that, because I think it's silly.. however it is my task to implement it. If only to remove some casual "theives" ... :roll:

Posted: Thu Dec 15, 2005 2:53 pm
by Ambush Commander
It's a thorny issue since there a lots of versions of JavaScript and its syntax is very lax (I absolutely hate how they made semicolons optional). I wish you good luck.

Posted: Thu Dec 15, 2005 6:22 pm
by feyd
it's easy to fix the "bad" Javascript.. just run it through the JSLint tool ;) Granted, some of the stuff it may whine about are, in fact, invalid due to how the script was coded, but a huge percentage of the time, it's cries are bang on.

Posted: Thu Dec 15, 2005 6:45 pm
by Ambush Commander
Mmm... JSLint looks really cool.

Posted: Thu Dec 15, 2005 8:11 pm
by AKA Panama Jack
Check this out...

http://dgrin.com/showthread.php?t=22928

It is very simple and it does work.

BTW, if you can get the server admin to enable gZip on the server itself for all http output then everything including your javascript will be compressed automatically.

The other thing is to just use the include function in the php script to include the js (as long as they are not applets) and css files (just remember to properly delimit them with ?><? tags). Sure people will be able to SEE the source easily but it will be compressed automatically with the rest of the html being sent.