Home >Web Front-end >JS Tutorial >Can JavaScript Compress JSON Data Client-Side for Servers with Fixed-Size Caches?
JavaScript Implementation of Gzip
Question:
Is there a way to compress JSON data on the client side before sending it to a server with a fixed-size cache?
Answer:
Edit: For better results, use the improved LZW solution found at http://pieroxy.net/blog/pages/lz-string/index.html.
Original Solution:
While JavaScript does not provide an implementation of Gzip, the jsolait library offers functions for LZW compression and decompression. Here is the code:
LZW Compression Function:
function lzw_encode(s) { var dict = {}; var data = (s + "").split(""); var out = []; var currChar; var phrase = data[0]; var code = 256; for (var i = 1; i 1 ? dict[phrase] : phrase.charCodeAt(0)); dict[phrase + currChar] = code; code++; phrase = currChar; } } out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0)); for (var i = 0; i <p><strong>LZW Decompression Function:</strong></p><pre class="brush:php;toolbar:false">function lzw_decode(s) { var dict = {}; var data = (s + "").split(""); var currChar = data[0]; var oldPhrase = currChar; var out = [currChar]; var code = 256; var phrase; for (var i = 1; i
The above is the detailed content of Can JavaScript Compress JSON Data Client-Side for Servers with Fixed-Size Caches?. For more information, please follow other related articles on the PHP Chinese website!