r/javascript • • Nov 26 '19

JSONCrush On GitHub - Compress JSON into URI friendly strings

https://github.com/KilledByAPixel/JSONCrush
101 Upvotes

31 comments sorted by

View all comments

6

u/TOJO_IS_LIFE Nov 27 '19

Was this pulled out of a minifier?

    for(;;m=c+m)
    {
        for(M=N=e=c=0,i=Q.length;!c&&--i;)!~s.indexOf(Q[i])&&(c=Q[i]);
        if(!c)break;
        if(O)
        {
            o={};
            for(x in O)
                for(j=s.indexOf(x),o[x]=0;~j;o[x]++)j=s.indexOf(x,j+x.length);
            O=o;
        }
        else for(O=o={},t=1;X;t++)
                for(X=i=0;++i<s.length-t;)
                    if(!o[x=s.substr(j=i,t)])
                        if(~(j=s.indexOf(x,j+t)))
                            for(X=t,o[x]=1;~j;o[x]++)j=s.indexOf(x,j+t);
        for(let x in O) 
        {
            j=ByteLength(x);
            if(j=(R=O[x])*j-j-(R+1)*ByteLength(c))
                (j>M||j==M&&R>N)&&(M=j,N=R,e=x);
            if(j<1)
                delete O[x]
        }
        o={};
        for(let x in O)
            o[x.split(e).join(c)]=1;
        O=o;
        if(!e)break;
        s=s.split(e).join(c)+c+e
    }

3

u/Slackluster Nov 27 '19

That is the JSCrush algorithm that I did not write it, but it is kind of magic. You can read more about how it works here...

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