Compressing data with Javascript

Asked

Viewed 1,769 times

10

Is there any way to compress data (strings) using Javascript?

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

So I would like to know if there is any efficient way to compress data using Javascript only.

1 answer

11


The library lz-string seems a good option to compress 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>

Just be careful when choosing the correct method of represent the result compacted depending on what you will do with it.

  • The pattern compress is specific to the Webkit (to save in the Storage location), will not work in other environments - much less when there is a server involved;
  • The option compressToUTF16 seems to me the most appropriate, because the result is a common string without encoding problems (encoding). However, check what the server will do with this string, because if it represents it in a less compact format (UTF-8, or maybe ASCII with "escaped" Unicode characters) it may end up occupying plus 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 you use Uint8Array is good, but you will have to encode it (serialize it) in some format before sending it to the server, unless this accepts binary data.
  • Our really impressive, had not yet seen something like +1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.