compressing string in js and save in localStorage
<p>I'm trying to save a HUGE json string in my localStorage but for some reason sometimes it saves it and sometimes not, I thought I should compress it so I took an <code>LZW implementation</code> in js from one of the threads in stackoverflow.</p>

<p>The problem is when I try to <code>localStorage.setItem()</code> the compressed string it gives me an error "<code>Invalid argument</code>", any idea why or what should I do?</p>

<p>Edit:</p>

<p>this is the code I'm using:</p>

<pre><code>// LZW-compress a string
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&lt;data.length; i++) {
        currChar=data[i];
        if (dict[phrase + currChar] != null) {
            phrase += currChar;
        }
        else {
            out.push(phrase.length &gt; 1 ? dict[phrase] : phrase.charCodeAt(0));
            dict[phrase + currChar] = code;
            code++;
            phrase=currChar;
        }
    }
    out.push(phrase.length &gt; 1 ? dict[phrase] : phrase.charCodeAt(0));
    for (var i=0; i&lt;out.length; i++) {
        out[i] = String.fromCharCode(out[i]);
    }
    return out.join("");
}

// Decompress an LZW-encoded string
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&lt;data.length; i++) {
        var currCode = data[i].charCodeAt(0);
        if (currCode &lt; 256) {
            phrase = data[i];
        }
        else {
           phrase = dict[currCode] ? dict[currCode] : (oldPhrase + currChar);
        }
        out.push(phrase);
        currChar = phrase.charAt(0);
        dict[code] = oldPhrase + currChar;
        code++;
        oldPhrase = phrase;
    }
    return out.join("");
}
</code></pre>

<p>this is the code that calls the compressing algorithm and saves it in LS</p>

<pre><code>LOG("GetMerchantList(): Done");
    var SITESVAR = unescape(data)
    localStorage.setItem("MYSITES", lzw_encode(SITESVAR));  //this throws error
    IWT.BuildMerchantList(SITESVAR);
</code></pre>
