Compression and Decompression of data  

by ne on 2021-09-29 under Javascript

Many times we need to compress the data and we stuck on this how should we compress the data without losing any info.

Requirments:-
1.localStorage:- Now a days browser supports localstorage where we can put 5 MB(approx) data but some time we need to put more
data so then we need to compress the data and then we can store it on localstorage.

2.Performance:- suppose we need to send millions of records to the client from server then to enhance the performance we can
use compression logic to compress the data and decompress it on client side without losing any data.

there could be many ways to compress the data but LZ-string compresson logic one of them and its easy to use and integrate in our
project and its available for Java, C# and Javascript. see how we can use it in javascript:-

Example:-


var data= "coding lord is testing compression.";
alert("Size of sample is: " + data.length);

//here we are compressing the data
var compressed = LZString.compress(data);

//check the after compression
alert("Size of compressed sample is: " + compressed.length);

//here we are decompressing the data
data = LZString.decompress(compressed);

//check the size after decompression
alert("Sample is: " + data);



for more details you can visit the :- lz-string blog

Thanks