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

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

问题描述

我正在寻找一个 JavaScript 函数,它给定一个字符串,返回一个压缩的(较短的)字符串.

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

我正在开发将长字符串 (HTML) 保存到本地数据库的 Chrome 网络应用程序.出于测试目的,我尝试压缩存储数据库的文件,但它缩小了五倍,因此我认为如果我压缩它存储的内容,这将有助于保持数据库更小.

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.

我在 JavaScript 中找到了 LZSS 的实现:http://code.google.com/p/u-lzss/(U-LZSS").

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

当我使用简短的示例字符串(解码 === 编码)手动"测试它时,它似乎有效,而且在 Chrome 中它也相当快.但是当给定大字符串(100 ko)时,它似乎会混淆/混淆字符串的后半部分.

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.

U-LZSS 是否有可能需要较短的字符串而无法处理较大的字符串?是否可以调整一些参数以移动该上限?

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?

推荐答案

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

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. 有效
  2. 它将数据库的大小减少了两倍

... 小于 5 但总比没有好!所以我用了那个.

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

(我希望我能接受 Piskvor 的回答,但这只是评论).

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

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

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