javascript string compression with localStorage

前端 未结 5 1585
感情败类
感情败类 2020-12-23 17:53

I am using localStorage in a project, and it will need to store lots of data, mostly of type int, bool and string. I know that javascript strings are u

5条回答
  •  抹茶落季
    2020-12-23 18:20

    "when stored in localStorage, do they stay unicode?"

    The Web Storage working draft defines local storage values as DOMString. DOMStrings are defined as sequences of 16-bit units using the UTF-16 encoding. So yes, they stay Unicode.

    is there a way I could compress the string to use all of the data in a unicode byte...?

    "Base32k" encoding should give you 15 bits per character. A base32k-type encoding takes advantage of the full 16 bits in UTF-16 characters, but loses a bit to avoid tripping on double-word characters. If your original data is base64 encoded, it only uses 6 bits per character. Encoding those 6 bits into base32k should compress it to 6/15 = 40% of its original size. See http://lists.xml.org/archives/xml-dev/200307/msg00505.html and http://lists.xml.org/archives/xml-dev/200307/msg00507.html.

    For even further reduction in size, you can decode your base64 strings into their full 8-bit binary, compress them with some known compression algorithm (e.g. see javascript implementation of gzip), and then base32k encode the compressed output.

提交回复
热议问题