Your Favorite Games

Loading...

Javascript String Compression

About this tutorial

Category: Javascript
Difficulty: Expert
Javascript String Compression.zip

Results

Saving is an important aspect of incremental games, and is covered in another tutorial, but what if your save data is quite large? With the advent of local storage, you are now given 5MB of storage space per domain. Never assume, however, that you have access to that total amount, as there may be other applications that are using this storage space.

So how do you work around these limitations? Compression, along with JSON, is key. In this tutorial, we will use a script available for free usage to compress our JSON string and save it to local storage, then load it back and uncompress it to display the information.

If you haven't yet learnt how to save data with JSON and classes, check out the Javascript Saving tutorial first.

We'll build on our previous tutorial, so you'll need to use either the files you created, or the downloadable package from that tutorial. We're going to add a script available from here. The script is free to use for your projects, and is the most efficient I've been able to find thus far. Note that we are not using the script in the main body, but a revision at the bottom of the screen in the comments.

The exact script we'll use is the following:

  1. // LZW-compress a string
  2. function lzw_encode(s) {
  3. var dict = {};
  4. var data = (s + "").split("");
  5. var out = [];
  6. var currChar;
  7. var phrase = data[0];
  8. var code = 256;
  9. for (var i=1; i<data.length; i++) {
  10. currChar=data[i];
  11. if (dict['_' + phrase + currChar] != null) {
  12. phrase += currChar;
  13. }
  14. else {
  15. out.push(phrase.length > 1 ? dict['_'+phrase] : phrase.charCodeAt(0));
  16. dict['_' + phrase + currChar] = code;
  17. code++;
  18. phrase=currChar;
  19. }
  20. }
  21. out.push(phrase.length > 1 ? dict['_'+phrase] : phrase.charCodeAt(0));
  22. for (var i=0; i<out.length; i++) {
  23. out[i] = String.fromCharCode(out[i]);
  24. }
  25. return out.join("");
  26. }
  27. // Decompress an LZW-encoded string
  28. function lzw_decode(s) {
  29. var dict = {};
  30. var data = (s + "").split("");
  31. var currChar = data[0];
  32. var oldPhrase = currChar;
  33. var out = [currChar];
  34. var code = 256;
  35. var phrase;
  36. for (var i=1; i<data.length; i++) {
  37. var currCode = data[i].charCodeAt(0);
  38. if (currCode < 256) {
  39. phrase = data[i];
  40. }
  41. else {
  42. phrase = dict['_'+currCode] ? dict['_'+currCode] : (oldPhrase + currChar);
  43. }
  44. out.push(phrase);
  45. currChar = phrase.charAt(0);
  46. dict['_'+code] = oldPhrase + currChar;
  47. code++;
  48. oldPhrase = phrase;
  49. }
  50. return out.join("");
  51. }
  52. function encode_utf8(s) {
  53. return unescape(encodeURIComponent(s));
  54. }
  55. function decode_utf8(s) {
  56. return decodeURIComponent(escape(s));
  57. }

You'll need to load this into your javascript file. It will handle everything for you, but you need to remember that we will be using UTF8 formatting, so if your users are going to be exporting, you'll need to let them know that they need to change their save file type or they may lose their data.

Implementing this is pretty simple. We take the JSON of our Game variable and first of all encode to UTF8 (just to be safe) then compress. Replace the following line:

  1. window.localStorage['SaveName'] = JSON.stringify(Game);

With this:

  1. var SaveGame = JSON.stringify(Game);
  2. SaveGame = encode_utf8(SaveGame);
  3. SaveGame = lzw_encode(SaveGame);
  4. window.localStorage['SaveName'] = SaveGame;

The above will create your JSON string, encode it as UTF8, then compress it and save it to local storage. To get it back, you need to do the reverse. Replace the following:

  1. window.GameTwo = JSON.parse(window.localStorage['SaveName']);

With this:

  1. SaveGame = window.localStorage['SaveName'];
  2. SaveGame = lzw_decode(SaveGame);
  3. SaveGame = decode_utf8(SaveGame);
  4. window.GameTwo = JSON.parse(SaveGame);

The above retrieves your compressed data, uncompresses it, decodes from UTF8, then parses the string into the GameTwo variable. If you leave the rest of the file the same, you should find that the file works perfectly.