title: Most efficient way to store large arrays of integers in localStorage with Javascript
body: <blockquote>
    <p>*"Efficient" here basically means in terms of smaller size (to reduce the IO waiting time), and speedy retrieval/deserialization times. Storing times are not as important.</p>
  </blockquote>
  
  <p>I have to store a couple of dozen arrays of integers, each with 1800 values in the range 0-50, in the browser's localStorage -- that is, as a string.</p>
  
  <p>Obviously, the simplest method is to just <code>JSON.stringify</code> it, however, that adds a lot of unnecessary information, considering that the ranges of the data is well known. An average size for one of these arrays is then ~5500 bytes.</p>
  
  <p>Here are some other methods I've tried (resultant size, and time to deserialize it 1000 times at the end)</p>
  
  <ul>
  <li><p>zero-padding the numbers so each was 2 characters long, eg:</p>
  
  <pre><code>[5, 27, 7, 38] ==&gt; "05270738"
  </code></pre></li>
  <li><p>base 50 encoding it:</p>
  
  <pre><code>[5, 11, 7, 38] ==&gt; "5b7C"
  </code></pre></li>
  <li><p>just using the value as a character code (adding 32 to avoid the weird control characters at the start):</p>
  
  <pre><code>[5, 11, 7, 38] ==&gt; "%+'F" (String.fromCharCode(37), String.fromCharCode(43) ...)
  </code></pre></li>
  </ul>
  
  <p>Here are my results:</p>
  
  <pre><code>                  size     Chrome 18   Firefox 11
  -------------------------------------------------
  JSON.stringify    5286          60ms         99ms
  zero-padded       3600         354ms        703ms
  base 50           1800         315ms        400ms
  charCodes         1800          21ms        178ms
  </code></pre>
  
  <p>My question is if there is an even better method I haven't yet considered?</p>
  
  <p><strong>Update</strong><br>
  MДΓΓБДLL suggested using compression on the data. <a href="https://stackoverflow.com/a/294421/9021">Combining this LZW implementation</a> with the base 50 and charCode data. I also tested aroth's code (packing 4 integers into 3 bytes). I got these results:</p>
  
  <pre><code>                  size     Chrome 18   Firefox 11
  -------------------------------------------------
  LZW base 50       1103         494ms        999ms
  LZW charCodes     1103         194ms        882ms
  bitpacking        1350        2395ms        331ms
  </code></pre>