最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Text compression in JavaScript - Stack Overflow

programmeradmin2浏览0评论

Are there any libraries/etc. that implement lightweight text compression in JavaScript?

My real goal is to slightly shorten some text and make it inconspicuous at a glance. (It needn't provide security -- the text will be displayed for the user.) Ideally the compression could be tweaked to output only the query characters

[-._~!$&'()*+,;=:@/?a-zA-Z0-9]

so it can be passed in a URL. (Actually, this is kind of important -- if other characters are used they'd have to be percent-encoded, which will probably make the text larger than the original.) Of course rare characters could be percent-encoded if needed.

Any ideas? Failing that, suggestions for making a simple one? The compression doesn't have to be great but shorter URLs would be nicer. The text to be compressed should be English sentences: mostly lower-case letters, spaces, and punctuation with the occasional upper-case letters, digits, and newlines.

Are there any libraries/etc. that implement lightweight text compression in JavaScript?

My real goal is to slightly shorten some text and make it inconspicuous at a glance. (It needn't provide security -- the text will be displayed for the user.) Ideally the compression could be tweaked to output only the query characters

[-._~!$&'()*+,;=:@/?a-zA-Z0-9]

so it can be passed in a URL. (Actually, this is kind of important -- if other characters are used they'd have to be percent-encoded, which will probably make the text larger than the original.) Of course rare characters could be percent-encoded if needed.

Any ideas? Failing that, suggestions for making a simple one? The compression doesn't have to be great but shorter URLs would be nicer. The text to be compressed should be English sentences: mostly lower-case letters, spaces, and punctuation with the occasional upper-case letters, digits, and newlines.

Share Improve this question asked Jul 22, 2011 at 14:13 CharlesCharles 11.5k14 gold badges69 silver badges111 bronze badges 7
  • 2 Rather than compressing the query params, maybe you should try URL shortening services. – Mrchief Commented Jul 22, 2011 at 14:15
  • 2 Compressing short text doesn't pay off much... – Ondra Žižka Commented Jul 22, 2011 at 14:18
  • given that you want to compress English language sentences, and the characters in 95% or more of most sentences are included in your set of acceptable characters, you may not gain much beyond stripping whitespace. What's your motivation here? That may help determine an appropriate method. – Surreal Dreams Commented Jul 22, 2011 at 14:20
  • @Ondra Žižka: Not so, considering my use case. I'd use ROT13 just for light obfuscation if I wasn't going to compress; any shortening on top of that is just a bonus. – Charles Commented Jul 22, 2011 at 15:19
  • 1 Chrome 80 will provide CompressionStream API, see blog.chromium.org/2019/12/… – 4esn0k Commented Dec 22, 2019 at 12:47
 |  Show 2 more comments

2 Answers 2

Reset to default 18

I've found lz-string which is a perfect fit for my needs. It compresses and decompresses text rapidly and can target raw bits, valid Unicode characters, or Base64. Perhaps it will be useful for someone else?

I was thinking too of a huffman compression. This javascript library is really good: http://rumkin.com/tools/compression/compress_huff.php. It has a really space saving method to compress and decompress each character. Maybe you want to look for a Golomb Code, too. It has about the same compression rate like the huffman but IMO it's easier to implement. Don't ask me about an example.

发布评论

评论列表(0)

  1. 暂无评论