back
62 comments
This looks interesting, but I wish there was an explanation of how it works; I'm particularly curious about how it extracts repeated substrings. I tried reading the code, but it starts off with:

  let X, B, O, m, i, c, e, N, M, o, t, j, x, R;
and doesn't get any more readable from there. (And that's the unminified version!)

There aren't any tests, either, so if you're using this and find a bug I guess you just have to hope that nothing breaks when you change a line like

  for(M=N=e=c=0,i=Q.length;!c&&--i;)!~s.indexOf(Q[i])&&(c=Q[i]);
Sorry, that part of the code is from JSCrush which I did not write. I don't fully understand it but here's an explanation someone wrote...

https://nikhilism.com/post/2012/demystifying-jscrush

If you go to the live demo, it will do a test to crush/encodeURI and uncrush/decodeURI to the string you pass in and verify that they are the same. Maybe I should write some automated tests.

Did someone actually write that or did it come from a minified output?
I assume they minified it, but I don't have access to the unminified source. I'd like to clean it up or find a cleaner version, but for now it works well enough.
I guess you/someone could try a re-implementation.
Rison (https://github.com/Nanonid/rison) is a nice alternative that seems to be more readable IMHO.
Yes I use this one, not as compact but it remains readable/editable, more notably Kibana uses it as well.
thank you. I was running into a JSON parse error on a script for a couple of days. I love JSON but this compact notation using rison helped me quickly debug the error! :-)

Thanks again!

Looks cool, I just checked into it. It does work well, but doesn't appear do anything to reduce repeated substrings. Maybe a combination of the two would work well though.
I think a more 'popular' pattern is is to base64 encode the JSON. This is how we do it (tracking pixels/get requests), and lots of analytics/ad tech uses that pattern as well. But I'm not sure on the compression/size.
Encoding to base64 increases the size by at least 33%. This is to help make links small enough to share on places like twitter, dischord etc where the max length is around 4000 bytes.
If you compress the json first, it isn't quite so bad.

Example #1 (short string):

  input: 103 bytes
  gzip(input): 87 bytes
  base64(gzip(input)): 117 bytes
Example #2 (long string):

  input: 3122 bytes
  gzip(input): 840 bytes
  base64(gzip(input)): 1121 bytes
Thanks, that is good reference. It makes sense that Zip would beat out JSON for longer strings. Just as another point of comparison, how about encodeURIComponent(gzip(input))?
This project also blows up the size and it seems by much more than 33% on average. You'd safe some space by using base64.
Hold my beer!

...

https://donohoe.dev/project/jspng-encoder/

Life is too short to do something useful so why not encode all your site's Javascript code into a PNG image and then decode it on demand.

(This is a terrible idea. Don’t do it. Just for fun)

this is quite cool. how do you convert the file to a png?

Why is this a terrible idea?

I'm thinking I can Whatsapp friends a huge letter as an image and they could use my toy app to decode it ;-)

A PHP script does the conversion, basically takes the code converts to a base64 png.
Awesome project.
The code is a bit dense for my taste, these are extracts from the unminified version:

for(M=N=e=c=0,i=Q.length;!c &&--i;)!~s.indexOf(Q[i])&&(c=Q[i]);

RegExp(`${(g[2]?g[2]:'')+g[0]}|${(g[3]?g[3]:'')+g[1]}`,'g');

This is the JSCrush algorithm that I did not write and it is kind of magic. The uncompressor is crazy small. You can read more about it here...

https://nikhilism.com/post/2012/demystifying-jscrush

The zzart format looks ridiculously wordy. A lot of compression could be achieved by shortening the property names. I wonder how much BSON would provide in size advantage but it'd have to be encoded to something amenable to a URI.
Here's another approach that compresses even better while retaining (limited) human readability: https://bitbucket.org/tkatchev/muson/

(Though it's mostly meant for making things smoother in statically-typed languages.)

are they usable in a URI though?
No, you'll need to percent-encode them first.
Maybe further saving could be done by converting to cbor before urlencode
Idea: just put the thing in localstorage? It's cheaper, less fiddly, you can encrypt it with something like TEA if you so desire, and doesn't make urls unshareable by existing
> and doesn't make urls unshareable by existing

But when you put json in the URL, you normally do want exactly that. Making the URL shareable.

Yes, localstorage is great, but this for making URLs that are short enough to share on twitter, dischord, etc. The max twitter url length is ~4000 characters I think.
I was looking for something like this last week so was interested to try this. It's just crashed in Firefox with the fans also kicking up to max. It worked in Chrome with a 31% reduction in size, but took a while, and the fans kicked in again.
I had some issues with long strings, like 5000 or greater. What length string were you using?
22728 characters! Maybe that's way bigger than this is meant for.
Yes, I'm interested in trying to make it work with longer strings. Worst case scenario if it gets long strings it could just split it up into strings of length that it can handle. The speed seems to decreases rapidly with string length at some point eventually hitting a brick wall, the sweet spot seems around 1000-5000.
Why?
I have a personal project that saves state to the URL's hash (https://www.rebalancecalc.com).

I do that because I wanted users to be able to save their state without burdening those users with accounts or burdening myself with maintaining a DB for something so simple.

I also somewhat abuse the history API and use my "read the URL and load state" logic to implement undo-redo via navigating back and forth, though that doesn't seem to work right now. I am working on a refactor that uses redux to implement undo-redo and just replace state, to keep the user's history clean.

Storing encoded JSON in the URL hash is a nifty hack in my opinion. Users can save state in a bookmark or share it with others easily, and it's clear to the users that "where they are" in the URL bar maps to the current app state. Plus, bookmark syncing is taken care of by most browsers to make that state available elsewhere, etc. For the site owner, it means not needing a DB to make an app with some kind of state persistence.

One risk: be sure the state you persist to the URL is in a schema you plan to retain compatibility with! Blind serialization and de-serialization is a recipe for bugs and misery the next time you add a feature.

I do the same on a production app at work, but for a different reason.

Storing confidential data in the hash assures my users that I (the developer) don’t have access to this data, since anything after the hash never gets sent to the server by the browser.

It wouldn’t stop me from sending that information with a post request afterwards but the code is open source and it could be noticed in developer tools.

Sorry I don't understand, you put confidential data into an URL so it's probably in server logs etc.?
JSON is pretty ubiquitous for transferring data between web resources, compressing the data with a novel method is always welcome.
Ingress and egress costs are expensive in the cloud. Using this with gzip could save you on cost.
One use case where I've put (uri-encoded) JSON in the query string is for parameterizing GET requests with more structured data, since URL encoding is fairly limited and GET requests don't have a body like POSTS.
You can probably use extended URL encoding for that, which is supported by at least body-parser [https://npmjs.com/package/body-parser]. Not sure about other PLs.
Can't you just base64 url safe encode it and get the same result? Aren't URl strings limited in their length as well?
As far as I know base64 is not guaranteed to be uri safe, though most of the time you should be fine. However more importantly converting to base64 automatically increases the size by 33%

https://developer.mozilla.org/en-US/docs/Web/API/WindowBase6...

As others mentioned, size is the main concern here, which is also why JSONCrush could be useful. But I'd definitely use POSTs first if possible.
If you don't care about IE and Edge you can fit 8KB-64KB in the URI, otherwise 2K is pretty safe.
It may not apply in your case, but in some cases, you can give a GET request a body - Elasticsearch relies on this.
From what I've seen, many intermediaries do nasty things if you have a body on anything that doesn't 'normally' have one. I've heard a fair amount about GCP's load balancers dropping unexpected bodies, and other tools giving a 5XX.

It is really unfortunate, because there are tons of use cases and zero reason to interfere with these requests.

The justification is also downright ridiculous. The argument is that "GET, DELETE, ... have no defined semantics for bodies". Meanwhile the 'defined semantics' for POST bodies is... whatever the application decides.

I like this as a compression method, but not sure how I feel about sticking it in a URL.
It worked great for my use case http://zzart.3d2k.com

Reduces share urls by about 75% for these very repetitious JSON strings.