Just released: lz-string
lz-string is a compression program for JavaScript in the browser, based on LZW. It is fast, meant for localStorage or any other string-based storage in JavaScript, and is particularly efficient on short strings.
All can be read on the home page: https://pieroxy.net/blog/pages/lz-string/index.html And a demo can be found below for live compression: https://pieroxy.net/blog/pages/lz-string/demo.html
Lua decompress
I implemented the decompression algorithm in Lua 3.2, but it takes too long to complete the process. I do not understand why this happens. But data compression is extremely fast.
[Compress / Decompress] Python
[Decompress only] Lua 3.2
Data is compressed into Python, and decompressed on the Lua. I wanted to use lzstring in a communication network (RPC).Lua decompress
Re: Just released: lz-string
This implementation/upgrade of LZW is genius!! Specially for Chrome since the localStorage stores every single byte as a word (2 bytes) occupying twice as much space hence instead of having 5MB of storage it gets reduced to 2.5MB.
C'est génial!
Re: Just released: lz-string
This is not specific to Chrome, but all browsers AFAIK. localStorage stores JavaScript Strings, which are UTF-16, meaning every character takes up 16bits of space. localStorage is limited effectively to 5MB, even on Chrome, it's just that Strings use a lot of memory in JavaScript. Note that this is not specific to JavaScript as it is the same in Java and C# for example.
The real issue I tried to solve in JavaScript is "how to represent a binary stream of data". The best answer I found is using Strings and using the 16-bits of all characters as storage.
Re: Just released: lz-string
hi
Great job !!!
In my application I would like get from server compressed template data. Do you think about implementing this algorithm in java?
Re: Just released: lz-string
Just turn on gzip compression on your server and everything your server sends to the browser should be already gzip-compressed automatically, which is most likely much, much more efficient. You can see that if your server sends back a "Content-Encoding: gzip" into the http response to your browser. On Chrome, with the debugging tools turned on on the Network tab, you can see "Size/Content" for every request. Size is the actual number of bytes transferred while Content is the size of your uncompressed content.
Re: Just released: lz-string
FYI, I'm using LZString within a chrome-app (aka packaged app) and found that while using LZString.compress() to compress ~12MB of string before saving in chrome.storage.local, Chrome started suspending my chrome app unpredictably. Switching to LZString.compressToUTF16() fixed the suspend problem.
I suspect Chrome has some background storage checker that suspends extensions that store malformed data, like LZString compressed strings.
Re: Just released: lz-string
Thanks for the feedback. Thinking about it, LZString.compressToUTF16() seems much safer. Strings are sets of characters after all and who can predict what bugs an invalid character will trigger?
What is this chrome.storage.local you're using? If I read you correctly you store more than 5MB in it, so it must be something else than localStorage...
Re: Just released: lz-string
chrome.storage.local is storage API available to Chrome apps and extensions.
You can find documentation here: http://developer.chrome.com/apps/storage.html
Unlike localStorage, it's async. It's still limited to 5MB.
Good news is that I've managed to cut down my data to ~3.7M *characters* which,
using LZString compresses down to 10% (w00t). Bad news is, when I call
chrome.storage.local.getBytesInUse, returned number is ~2.5M *bytes* which,
if correct, suggests chrome.storage.local implementation doesn't just store what it
receives but transforms it somehow such that much of the compression magic gets
diffused. It could be that localStorage does the same.
Re: Just released: lz-string
I found myself needing a Java implementation of this for pre-compressing data from a Java program before putting it on a static web-server. I decided to not rely on the internal automatic compression of most web servers to reduce complexity.
I wrote one and put it on my github here if anyone is interested: https://github.com/ownaginatious/lz-string-java
Just a word of caution though; I haven't done a very extensive amount of testing.
Re: Just released: lz-string
Thanks for the effort. Just out of curiosity, why don't you rely on the webserver compression? That one is full fledge gzip which ought to compress a lot better than LZ-String...
Anyways, I'll link to your project from the documentation.
Re: Just released: lz-string
Re: Just released: lz-string
Re: Just released: lz-string
Well, for my specific application it was crucial that data get compressed before being sent to clients. From what I understand, if I were to rely on the internal gzip of the webserver, there is a chance that some devices wouldn't support it and would request the uncompressed version, which would waste a lot of bandwidth. This kind of "guarantees" that the compression will always happen. The content I'm serving is already stored as files compressed using your algorithm :). I also wanted a method of sending compressed data back to a webserver, which from what I can tell isn't natively supported by many browsers.
I know that's an awfully specific purpose, but I figure someone else out there might find a clever use for a Java implementation :)
Also, I forgot to mention before - great library! Out of all the JS compression libraries I've looked through, yours is by far the easiest to use!
lzString works ok, breaks Chrome debug tools
I am experimenting with lzString to store compressed data from javascript simulations
LZString seems to be working fine
But... when I bring up the debug tools, and click over to the Local Storage tab, it hangs and
the debug tools can not be dismissed with the X.
The website keeps working though.
Local storage with uncompressed data, even though it is pretty big (250K chars), does not
break the chrome Local Storage viewing tab in the dev tools.
This is probably chromium's problem, not yours. I have not yet tried other browsers.
Just thought you might want to be aware of it, and wondering what it might be....
lzString works ok, breaks Chrome debug tools
I have noticed this behavior with a localStorage full (5MB of packed strings) but it doesn't freeze, it just takes an awful lot of time rendering those weird strings. If you look at your task manager (or a top in a terminal) while trying to open the dev tools, you'll notice that one full core is crunching data for Chrome. This is the rendering part. while the rendering is done you can interact with your dev tools again.
Firebug exibits the same behavior
This has to do (I think) with the handling of ultra-long strings coupled with the rendering of obscure UTF16 characters. With just a few kilobytes in localStorage everything is instantenuous.
Re: Just released: lz-string
You're an absolute life saver, extremely easy to use and opens up lots of possibilities with cookies.
Re: Just released: lz-string
Hi! I just wanted to leave a comment to say thanks for writing and releasing this - as you found out yourself, there really isn't anything else comparable that is readily available! I wanted to be able to reduce the size of JSON data stored on the server and decompress it on the client and this has enabled me to do just that. I was hosting on NeoCities which has a limit on the size of hosted sites - so relying on gzip for transmission was no good as that compresses the content delivered over the wire but doesn't reduce the backend storage requirements. Shameless plug: I wrote about this at JavaScript Compression (Putting my JSON Search Indexes on a diet). But really I just wanted to say thanks. So thanks! :)
nodejs error ?
I'm testing a node client <--> server chat program, and was wondering if your script was working for sending base64Strings.
It is working and it is not working :S ;)
A simple test gives me this:
var stringex = "This is my compression test.";
console.log("Size of sample is: " + stringex.length);
var compressed = Base64String.compress(stringex);
console.log("Size of compressed sample is: " + compressed.length);
string = Base64String.decompress(compressed);
console.log("Base64String Sample is: " + string);
var compressed3 = LZString.compressToUTF16(stringex);
console.log("Size of compressToUTF16 sample is: " + compressed3.length);
string = LZString.decompressFromUTF16(compressed3);
console.log("compressToUTF16 Sample is: " + string);
output:
Size of sample is: 28
<div> </div>Size of compressed sample is: 10
Base64String Sample is: ThisismycompressiontestA
Size of compressToUTF16 sample is: 17
compressToUTF16 Sample is: null
nodejs error ?
So, I got around to it and I tested my lib on nodejs.
Your first example (using Base64String.compress) cannot work as Base64String is meant to compress base64 encoded content. Your string ("This is my compression test.") is not a valid base64 string. So it doesn't work. Basically Base64String is meant to reencode Base64 content (usually images). The rationale is that base64 takes up a full character to store 6 bits, while compressToUTF16 stores 15 bits per characters.
Your second example is more useful in that really, it doesn't work. It looks as if your compressed test string triggers a bug in either compressToUTF16 or decompressToUTF16. I'm working on it as soon as I get home.
nodejs error ?
I just released version 1.3.3. Version 1.3.2 was the result of a pull request that I obviously didn't test enough...
nodejs error ?
Oke thanks,
I will use is like this:
Serverside:
LZString.compressToUTF16(JSON.stringify({JSON:DATA}))
Clientside:
data = JSON.parse(LZString.decompressFromUTF16(data.compr));
Well test more later.
nodejs error ?
That's the idea. How will you get the resulting encoded string on the client side? Ajax.responseText ?
nodejs error ?
Something like that, using http://socket.io/ but I can only send UTF-8 (and no binary) :S
compressToUTF16() works still, but maybe not for all messages...
Maybe I can create a UTF16 detection
IF( ContainsUTF16Char( LZString.compressToUTF16(JSON.stringify(args[1])) )){
nocompression
}
nodejs error ?
I'm not worried about encoding problems here, but I am worried that a UTF-8 encoded string is going to be substantially bigger than the UTF-16 counterpart LZString is producing, resulting in wasted bandwidth. Your UTF16 detection is going to return true all the time : compressToUTF16 *is* generating UTF-16 characters as its name can tell.
The best way would probably be to generate ISO-(LATIN-1 for example) characters (all 256 of them being valid), or UTF-16. But for this to be "optimal" you need the content type to be set correctly. Do you have the hand on the content encoding of these requests? If yes, I suggest switching to "Content-Type: text/html; charset=utf-16" for a better bandwidth usage.
If not, we'd need to write a compressToUTF8, using 7 bytes per character. Not quite hard.
The ideal solution would be for socket.io to be able to transfer byte arrays instead of strings. After all, we're trying to transmit binary data, not text.
nodejs error ?
Haha thanks for the information, I don't know very mucht about UTF8 / UTF-16
Socket.io don't support binary yet :(
If you think its easy to create a compressToUTF8() than I say :)
I quick compression check gives me this
msg = {data:'I say hello'}
console.log('no compression',JSON.stringify(msg).length)
compr = LZString.LZString.compressToUTF16(JSON.stringify(msg));
console.log('compression',compr.length);
no compression 22
compression 17
msg = {data:'I say hello, I can say more and more text so this is big'}
<div> <div>no compression 67</div> <div>compression 37</div> <div> </div> <div>As you can see there is some compression.</div> <div> </div> <div>But if I understand you correct , you can make more compression using compressToUTF8() ?console.log('no compression',JSON.stringify(msg).length)
compr = LZString.LZString.compressToUTF16(JSON.stringify(msg));
console.log('compression',compr.length);
</div> </div>
nodejs error ?
You are confused between two very different things: compression and encoding.
Compression is the act of taking bits as an input and outputing less bits on the output. This is where the LZ part of LZString is working.
Encoding is the act of taking bits as input and outputing characters as output. This is where the String part of LZString is working. This is needed because JavaScript doesn't know (consistently at least) how to handle binary data. It can handle numbers and strings.
So no, you cannot "make more compression" using compressToUTF8(). compress returns to you a UTF-16 string that is packed at the maximum but all 65536 values of a 16-bit int are not valid, so the string is indeed an invalid UTF-16 string. This is why I created compressToUTF16 that will only store 15 bits per character (hence, a slightly less optimal encoding) but produces a valid string. compressToBase64 gives you a string where only 6 bits are used for each characters. You might think that's complete bullshit as the string will be more than twice as big as a string produced with compressToUTF16, and yet is it the most efficient way to upload your data to your server, because everything will pass through the "url encoding" encoder, making every character outside the Base64 range three bytes at least.
Now, to exchange data, computers usually use bytes, not strings. UTF-8, ISO-8859-1 and UTF-16 are methods (encodings) to represent a string as a byte stream. A 1024 characters string may be represented as 1024 bytes in ISO-8859-1 but 2048 bytes in UTF-8. Similarly, another string may be represented as 1000 bytes in UTF-8 but 2000 bytes in UTF-16. Another, 1000 bytes in UTF-16 and 1500 bytes in UTF-8. So you have to know what you're doing in order to optimize your stream of data. Just calling "length" on your strings gives you the number of characters, not the number of bytes needed to tranfer thesse characters to your browser.
So, yes, anyone can write a compressToUTF8() method (it is actually very simple). But in UTF-8, the first bit is reserved for higher characters. So you can only store 7 bits per characters, wasting a full 12.5% of bandwidth, where with UTF-16 I only waste half of that to be UTF-compliant. I am sure you can set the content-type of your requests with your library, so why not just setting it up to be UTF-16 and be over with it? You save bandwidth and time, to get a more elegant solution.
But if you use compressToUTF16 and send it out encoded in UTF-8, your stream may very well end up being twice as big as it needs to be. Again, only testing will tell.
Ajax
hello , thanks for information.
i am using <a class="longLink" href="https://github.com/ownaginatious/lz-string-java" style="text-decoration: none; color: rgb(85, 130, 186); font-weight: bold; display: inline !important; overflow-x: auto; border: 1px solid rgb(221, 221, 221); background-color: rgb(238, 238, 238); padding: 1px; font-family: 'Trebuchet MS', 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif; font-size: 16px; line-height: 24px;">https://github.com/ownaginatious/lz-string-java code for compress json file (UTF-16) , i want send compress data with ajax to client and use javascript LZ-String for decompress ,
when data send to client javascript undefine variable for data.
please help me.
Re: Just released: lz-string
Thanks for helping me with this haha, its hard to understand it :)
What i'm testing right now is this:
Sending message function(){
msg = JSON.stringify(args[1]);
compres = LZString.LZString.compressToBase64(msg);
if(Buffer.byteLength(compres, 'base64')<Buffer.byteLength(msg, 'utf8')){
<span class="Apple-tab-span" style="white-space:pre"> </span>console.log('COMPRESSION');
args = ['event',{compr:compres}];
}
}
http://nodejs.org/api/buffer.html#buffer_class_method_buffer_bytelength_string_encoding
So it checks if the compression is smaller then the utf-8 message.
Re: Just released: lz-string
Wow... I can see quite a few mistakes here. First, you compress your content and then encode it in Base64 twice... Once with compresstoBase64 and once with Buffer.byteLength(..., 'base46'), hence, multiplying by ~1.8 its size. Looks like you need to read a bit about what is an encoding and what is compression.
Second, and this is a general advice whenever asking a question over the internet, we have no clue what you are trying to achieve. I mean, apart from calling LZString. What is it you want to do with LZString? Without this information, it's really hard to help you.
At last, what it looks like is that you're trying to compress some data on your server in order to send it to your client. Are you aware that pretty much all browsers and all webservers natively support compression? Furthermore, this is transparent, automatic and use gzip which is a far far better compression algorithm than LZString.
As I wrote in the documentation, LZString is meant to be used inside the browser. Of course, you can use it for other purposes, but then, be careful. It wasn't meant for that.
Re: Just released: lz-string
<span style="font-family: 'Trebuchet MS', 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif; font-size: 16px; line-height: 24px; background-color: rgb(240, 240, 240);"> Wow... I can see quite a few mistakes here. First, you compress your content and then encode it in Base64 twice... Once with compresstoBase64 and once with Buffer.byteLength(..., 'base46'), hence, multiplying by ~1.8 its size. Looks like you need to read a bit about what is an encoding and what is compression.</span>
?? <span style="background-color: rgb(240, 240, 240); font-family: 'Trebuchet MS', 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif; font-size: 16px; line-height: 24px;">Buffer.byteLength(..., 'base46')</span> gives me the compression length
<span style="font-family: 'Trebuchet MS', 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif; font-size: 16px; line-height: 24px; background-color: rgb(240, 240, 240);">Buffer.byteLength(msg, 'utf8') gives me the non-compression length</span>
If the compression is smaller then I send the compressed data to the client using socket.io (websocket/flash/ajax/etc)
If the compression is not smaller then I send the non-compressed data to the client using socket.io
Socket.io don't use <span style="background-color: rgb(240, 240, 240); font-family: 'Trebuchet MS', 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif; font-size: 16px; line-height: 24px;">gzip for the messages.
So that's why i'm searching for a compressed method to send the messages to the clients and back using javascript.
I works now great, with some small compression in some text messages.</span>
C# Class implementation
C# Class implementation
Re: Just released: lz-string
Many thanks for lz-string, its working great!!!
I'm using it from GWT. I needed to code this tiny wrapper:
Including the lz-string.js file is host page. :-)
Re: Just released: lz-string
Re: Just released: lz-string
Re: Just released: lz-string
CompressToUTF8
CompressToUTF8
CompressToUTF8
CompressToUTF8
Re: Just released: lz-string
Re: Just released: lz-string
Re: Just released: lz-string
Re: Just released: lz-string
Great to hear you found out about the issue. Thanks for the update.
Re: Just released: lz-string
Re: Just released: lz-string
Re: Just released: lz-string
Re: Just released: lz-string
Re: Just released: lz-string
Re: Just released: lz-string
Re: Just released: lz-string
Re: Just released: lz-string
Base128 really is a corner case, so I don't think it should be incorporated in the lib itself. It can be incorporated as an "add-on" for extra encoding options. You can submit a patch for a file called "lzstring-encoding.js" for example. I might add some other URL encoding as well in there. That sounds like a good idea.
Re: Just released: lz-string
Minified lz-string-x.y.z.js
Have you tried this? Are you interested in taking the code back if I make the necessary modifications?
I'm looking at including it in an application where we will use the Closure compiler to minify and concatenate it with other code.
Minified lz-string-x.y.z.js
Re: Minified lz-string-x.y.z.js
This has nothing to do with node.js and I don't know whether the last three (3) lines are relevant, yet.
Re: Minified lz-string-x.y.z.js
Re: Minified lz-string-x.y.z.js
I should have just compressed it first, however, our experience has been that code that passes JSLint always works when minified and code that doesn't regularly - though not always - has issues that are not always obvious.
Our standard has been to JSLint all code to increase our odds that all code paths are Ok, not just the ones we've managed to test (min'd vs orig).
I'd still encourage you to JSLint the source as it will minimize the chances there is code that will not minify gracefully.
Arabic characters
Arabic characters
Arabic characters
Arabic characters
Last question: Is this on a page on the internet where I can have a look?
Arabic characters
decompressFromBase64 is null
decompressFromBase64 is null
Java problem?
Java problem?
Also, as there are two Java ports, you can always try both.
Re: Just released: lz-string
Re: Just released: lz-string
Remember, LZ-String was always meant for small strings compression, not arbitrary length binary data.
Re: Just released: lz-string
Re: Just released: lz-string
The whole point of LZString is that it produces Strings. That's even in the name of the lib. What you do with those strings is up to you.
Re: Just released: lz-string
Re: Just released: lz-string
Re: Just released: lz-string
Re: Just released: lz-string
Re: Just released: lz-string
Re: Just released: lz-string
Re: Just released: lz-string
Re: Just released: lz-string
My code is much more faster than dioduailibe's implement which i think is too slow to be used on real production.
And dioduailibe's one is lack of [compressToBase64], which is excatly i need ...
Also, mine has a Rhino engine testcase.
With a 559KB large JSON to compress on i5-3570k @3.40GHz.
dioduailibe's code need 907ms.
Mozilla's Rhino engine need 649ms.(Which may be faster in continuous real production, 'cause Rhino env init slow. dioduailibe's is even slower than Rhino...)
my code need 117ms.
My code is at
https://github.com/rufushuang/lz-string4java
Can you give a link in the doc?
Re: Just released: lz-string
Re: Just released: lz-string
Re: Just released: lz-string
lz-string for python 3
lz-string for python 3
base64 – URI-safe?
I'm writing a single-page application that I want to be RESTful (everything necessary to build the model stored in URI) while holding multiple variables in query params. Some of these hold keys in indefinite lists, and I'd like to persist as many of these as possible while keeping the state bookmarkable without using a server-side service to generate a link to a JSON-encoded state – this becomes necessary when the URI reaches over 2000 characters.
Do you think it's safe to use base64 to encode and decode URI query parameters? So far everything seems to be working well but there may be edge cases I'm not aware of…
Thanks for a fantastically efficient & functionally complete plugin!
base64 – URI-safe?
base64 – URI-safe?
Re: Just released: lz-string
Possibility of compressing a JPEG image binary
Possibility of compressing a JPEG image binary
Just encode your JPEG in base46 and send it to your server.
Possibility of compressing a JPEG image binary
Possibility of compressing a JPEG image binary
15-bit compress/decompres
15-bit compress/decompres
That said, the compress method is pretty much useless since there's very little one can do with the result. So cloning the algo for compressToUTF16 probably makes sense. I'll get into that. Thanks.
15-bit compress/decompres
15-bit compress/decompres
Can you give me a hand and click on those JSPERF links so that I have a better sample to see if my changes are faster ?
Thanks
As for UTF-8 vs UTF-16, in JavaScript all strings are stored in UTF-16, so really, there is little choice.
Re: Just released: lz-string
Re: Just released: lz-string
It looks as if you've ported the latest version. You should really NOT port the latest version which is full of bad stuff to make the various JS engines happy.
Can you try porting version 1.0.2? This will be much much easier to debug (and easier to write). Here it is
Re: Just released: lz-string
Re: Just released: lz-string
Good! Now what we need to figure out if it is the decompression or the compression that doesn't work.
You have two strings: one that works and one that doesn't. Here is the result of the compression in Base46 of both your strings:
Can you post here the result of the compression of both strings by your library (in base64)?
Re: Just released: lz-string
Re: Just released: lz-string
Re: Long time it has been released: lz-string :)
Re: Long time it has been released: lz-string :)
I assume both previous comments were from you. What you need to check is that the String you are feeding decompressFromBase64() on the C# side is the exact same as the string you are getting from compressToBase64() from your browser. My guess is that they are not the same, because some of the characters in base64 need to be URL encoded.
You should use compressFromEncodedURIComponent(). This is like base64 but the characters chosen are slightly different (see the last two chars below). With a couple of replaces on the server side - String.replace("-","/") and String.replace("$","=") - you should get back the original base64 string. Just feed that to decompressFromBase64() in your C# app.
The characters used for base64: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=
The characters used for uricomp: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-$
If you still have issues, please open a bug into the C# repo (I'm not the author of the C# code)
IE8 Broken
IE8 Broken
IE8 Broken
I just released LZ-String 1.4.4 to fix this issue. There was also an issue with the demo page under IE8 and older which I fixed as well in the process. It now works under IE6.
Good catch. Thanks.
demo page over reports the uncompressed size
demo page over reports the uncompressed size
Internal representation of string in memory, localStorage, Indexed DB... are all using UTF16. These strings are effectively consuming two bytes per character, even for US-ASCII characters. Now, if encode those strings in UTF-8, I agree with you.
LZString is meant for the browser. Browser use UTF-16. localStorage quotas are by character, hence US-ASCII characters use 16 bits each.
This is why the demo page reflects this fact by counting all input characters as two bytes. Because they consume that many bytes.
demo page over reports the uncompressed size
demo page over reports the uncompressed size
Re: demo page over reports the uncompressed size
Re: demo page over reports the uncompressed size
It works or not depending on what you are trying to achieve. In JavaScript, all String are stored as UTF16, so all characters use up 16 bits of memory. That's the way it is and no matter what parameters you pass to lz-string, it will stay that way. Same goes for Indexed DB, localStorage and other JavaScript in-browser storage systems.
For example, storing 6 bits per character is important for String you are sending back to your server through a GET, because only 65 characters are allowed in there, the rest occupying 24 bits (at best) by being escaped in the form of %AB where AB is the ASCII code of your char in hexadecimal. Furthermore, those characters are being sent in UTF-8 (most of the time) and characters below 128 will occupy only 8 bits.
So it all depend on what you are trying to achieve with the result of the compression. Hence my question: What are you exactly trying to do with this?
Re: demo page over reports the uncompressed size
Re: demo page over reports the uncompressed size
Re: Just released: lz-string
Re: Just released: lz-string
Perl implementation ...?
Perl implementation ...?
Re: Just released: lz-string
Re: Just released: lz-string
The license for lz-string is literally that you can do whatever you want. Look up WTFPL in your favorite search engine.
This means that you can do whatever you want. If you want to give credit, by all means, do. If you don't want to, don't. If you want to put a link to this site, do. If you don't want to, don't.
You're on your own ;-)
Re: Just released: lz-string
Re: Just released: lz-string
I would suggest to prepend all versions (whether it's compressed or not) by one character. Let's say if you compress you prepend the result with 'z' and if you don't, you prepend with '_' for example. Then, on the decompress side, you check the first char of your string and decide if you should decompress or not depending on this char.
That said, if you plan on storing those strings locally (in the browser), no matter what size is you string you'll most likely get a smaller version with LZString.
Re: Just released: lz-string
Re: Just released: lz-string
Re: Just released: lz-string
Misleading but great
Misleading but great
Re: Just released: lz-string
Re: Just released: lz-string
Re: Just released: lz-string
Any way to "URI safe" a UTF16 compression?
Re: Just released: lz-string
Re: Just released: lz-string
strange chars decompressing a string
Help understanding the code
Question about _decompress() implementation
There is a loop at the beginning (line 346): which inserts some integers: 0, 1, 2 to dictionary array.
Later there are some strings added to this dictionary too (line 438, 456): Finally at the end of _decompress() there is condition (line 469): What is the purpose for this codition ?
Currently it returns false for dictionary[c] == null, c >= dictionary.length and for c=0 (because dictionary[0] contains 0).
Shouldn't the for loop at the beginning of _decompress() be like: so it would only contains strings ?
Different ports have different implementations:
diogoduailibe/lzstring4j: rufushuang/lz-string4java: jawa-the-hutt/lz-string-csharp: kreudom/lz-string-csharp:
Question about _decompress() implementation
The answer is in the switch(c). If the decompressor receives an entry 0, 1 or 2 it has a special meaning. All other entries IDs are to be taken from the dictionary. 0 means it's followed by an 8 bit entity. 1 by a 16 bit entity. 2 means it's the end of the stream. So put whatever you want in the first three entries, the code will never read from it. Or don't put anything. But start appending to your dictionary at index 3.
Let me know if you still have issues.
Re: Just released: lz-string
https://github.com/AmiArt/qt-lzstring
Some functions were not implemented, since I only needed UTF16 compression.
Done some optimalizations both in _compress() and _decompress() function.
Benchmark results:
Config:
Intel Core i7-2600 3.40GHz / Win10
MSVC++ 2010 / Qt 5.5.1
JSON file size 604 KB - compress() / decompress()
Firefox 49 - 265ms / 16ms
Google Chrome 53 - 218ms / 14ms
Internet Explorer 11 - 326ms / 14ms
qt-lzstring - 62ms / 11ms
lz-string is not great or even good
So to demonstrate what I mean (and just for fun) I have written a js LZW implementation in just such a way which can code any unicode string (hence the use of %, * and / just in case) as a valid UTF16 string (like your's ignoring the MSB and adding 32) which is about 50%-60% faster (yes, over twice as fast) encoding and is, I believe, suitably memory efficient and that has an indistinguishable decoding performance compared to lzString. It also has about a 3% improvement on compression. The code source is 2.5k (128 lines of well spaced code) and when minified (then prettyfied a bit) comes in at 17 lines long and around 1k and is included below. To test these claims, simply copy and paste the code as-is in a separate .js file, load as usual and substitute LZString.compressToUTF16(...), with lzw.Encode(...) and LZString.decompressFromUTF16(...) with lzw.Decode(...). Please note that this is not open source code and can only be legitimately used for test purposes. I reserve all rights, in the (probably vain) hope that others will devise their own efficient and elegant algorithms, hopefully more efficient and faster (but cuter?) than mine, and not persist in replicating badly wrought bloat-ware. Please also note that indexOf as used is horrendously slow in older browsers and would need to be replaced with a loop for an acceptable performance.
As a real challenge to lzString's ugliness, I have also written (but not included here) a “bells and whistles” js version that can output a native js array of bytes, a typed array of bytes, a char encoded string of bytes, a true base64 that is fully compatible with atob and btoa so when processed by these the result can be decoded without further modification, UTF16 and also a “lzstring” mode (i.e. potentially invalid UTF16) for comparison to your offering. It will encode any string or integer array (signed or not) and will return a string or array as per the original uncompressed data. This comes in at 254 lines long (6k) or about 2.5k minified and is about 40%-50% faster, but still with the 3% better compression, although it is possibly a few 1/1000ths slower than lzString when decoding, but this is hard to tell with any real certainty on a Windows platform using a WAMP.
Please write and publish better code in future.
lz-string is not great or even good
One thing I never did is insulting other authors.
lz-string is not great or even good
Re: Just released: lz-string
Re: Just released: lz-string
Re: Just released: lz-string
https://github.com/gsemac/lz-string-vb
Re: Just released: lz-string
Re: Just released: lz-string
Re: Just released: lz-string
Incorrect description for compressToEncodeURIComponent()
new port to kotlin
Re: Just released: lz-string
https://github.com/skipness/lzstring-dart
Re: Just released: lz-string
Re: Just released: lz-string
Re: Just released: lz-string
Re: Just released: lz-string
Re: Just released: lz-string
Re: Just released: lz-string