JavaScript中的字符串压缩 [英] String compression in JavaScript

查看:1708
本文介绍了JavaScript中的字符串压缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在开发一个可节省长字符串的Chrome网络应用程序。

我正在寻找一个给函数返回压缩(较短)字符串的JavaScript函数。 (HTML)到本地数据库。出于测试目的,我尝试压缩存储数据库的文件,并缩小了5倍,所以我认为这有助于保持数据库的小型化,如果我压缩存储的数据。



我在这里发现了JavaScript中的LZSS实现: http:// code .google.com / p / u-lzss / (U-LZSS)。

这似乎工作,当我手工使用简短的示例字符串(decode === encode),并且在Chrome中也相当快速。但是,当给予大字符串(100 ko)时,它似乎混淆了字符串的后半部分。



有可能U-LZSS期望短字符串并且可以不处理更大的字符串?为了移动这个上限,是否可以调整一些参数?

在Piskvor的建议下,我测试了代码在这个问题的答案中找到: Gzip的JavaScript实现
(顶级投票答案:LZW的实施),发现:


  1. 有效

  2. 数据库减少了两倍

...小于5但比没有好!所以我用了。



(我希望我能接受Piskvor的回答,但它只是一条评论)。


I'm looking for a JavaScript function that given a string returns a compressed (shorter) string.

I'm developing a Chrome web application that saves long strings (HTML) to a local database. For testing purposes I tried to zip the file storing the database, and it shrank by a factor of five, so I figured it would help keep the database smaller if I compressed the things it stores.

I've found an implementation of LZSS in JavaScript here: http://code.google.com/p/u-lzss/ ("U-LZSS").

It seemed to work when I tested it "by hand" with short example strings (decode === encode), and it's reasonably fast too, in Chrome. But when given big strings (100 ko) it seems to garble/mix up the last half of the string.

Is it possible that U-LZSS expects short strings and can't deal with larger strings? And would it be possible to adjust some parameters in order to move that upper limit?

解决方案

At Piskvor's suggestion, I tested the code found in an answer to this question: JavaScript implementation of Gzip (top-voted answer: LZW implementation) and found that:

  1. it works
  2. it reduces the size of the database by a factor of two

... which is less than 5 but better than nothing! So I used that.

(I wish I could have accepted an answer by Piskvor but it was only a comment).

这篇关于JavaScript中的字符串压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆