Compacting data with Javascript

8

Is there any way to compress strings using Javascript?

I'm developing something for a platform where it is not possible to use any server language (at least on their host) and we do not have access to the database. But I have found a way to save data using the platform itself, but sometimes I end up being limited by the limit of 60,000 characters they inflict.

So I wonder if there is any efficient way to compress data using only some server language.

    
asked by anonymous 28.12.2014 / 15:00

1 answer

10

The lz-string library looks like a good option for compressing strings via pure JavaScript:

var string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";

var compactada = LZString.compress(string);
var original = LZString.decompress(compactada);

// Níveis de compactação:
document.body.innerHTML += "Original: " + string.length + "<br/>";

var metodos = ["", "UTF16", "Base64", "EncodedURIComponent", "Uint8Array"];
for ( var i = 0 ; i < metodos.length ; i++ ) {
    var compactar = "compress" + ( metodos[i] ? "To" + metodos[i] : "" );
    var descompactar = "decompress" + ( metodos[i] ? "From" + metodos[i] : "" );
  
    var compactada = LZString[compactar](string);
    var original = LZString[descompactar](compactada);
    if ( original != string )
        alert("Erro ao descompactar!");

    var porcentagem = " (" + (compactada.length*100/string.length).toFixed(0) + "%)";
    document.body.innerHTML += compactar + ": " + compactada.length + porcentagem + "<br/>";
}
<script src="https://cdn.rawgit.com/pieroxy/lz-string/master/libs/lz-string.min.js"></script>

Justtakecarewhenchoosingtherightmethodtorepresentthecompressedresult,dependingonwhatyouaregoingtodowithit.

  • ThedefaultcompressisspecifictoWebKit(tosaveonlocalstorage),willnotworkinotherenvironments-letalonewhenaserverisinvolved;
  • ThecompressToUTF16optionseemstomethemostappropriate,becausetheresultisacommonstringwithnoencodingproblems(encoding).However,checkwhattheserverwilldowiththisstring,becauseifitrepresentsitinalesscompactformat(UTF-8,orperhapsASCIIwith"escaped" Unicode characters) it may end up occupying more space instead of less ...
  • The options that use base64 are the most "safe" (because the result is only ASCII), but the compression level is not the best ...
  • Finally, the one that uses Uint8Array is good, but you will have to encode it (serialize it) in some format before sending it to the server unless it accepts binary data.
28.12.2014 / 16:37