Reputation: 167
I am working on a plugin for an existing web-based tool in JavaScript. We are collecting data and store it in a string like this: "2.545,3.552,8.568;2.553,9.898,6.542;..." and so on.
The problem is, that we reach an export limit of 64k characters too fast. I wanted to ask - and excuse me if it is a dumb question - if someone knows of an algorithm or method we could use to compress the string before we export it. I am sure that it is technically possible but it certainly exceeds my skills as a programmer.
Thanks for any tips, link or suggestion.
Upvotes: 6
Views: 13066
Reputation: 97
You can use fflate
to Zip/Unzip a string.
In your HTML code add :
<script src="https://unpkg.com/[email protected]"></script>
In your JavaScript code add :
function zip_encode(str) {
const ascii = encodeURIComponent(str)
const array = new TextEncoder().encode(ascii)
const zip = fflate.deflateSync(array, {level: 9})
return window.btoa(String.fromCharCode(...zip))
}
function zip_decode(base64) {
const raw = window.atob(base64)
const array = Uint8Array.from(raw, c => c.charCodeAt(0))
const unzip = fflate.inflateSync(array)
const ascii = new TextDecoder().decode(unzip)
return decodeURIComponent(ascii)
}
This code should work efficiently for most use cases.
function zip_encode(str) {
const ascii = encodeURIComponent(str)
const array = new TextEncoder().encode(ascii)
const zip = fflate.deflateSync(array, {level: 9})
return window.btoa(String.fromCharCode(...zip))
}
function zip_decode(base64) {
const raw = window.atob(base64)
const array = Uint8Array.from(raw, c => c.charCodeAt(0))
const unzip = fflate.inflateSync(array)
const ascii = new TextDecoder().decode(unzip)
return decodeURIComponent(ascii)
}
// Example usage
const example = "Hello, this is a sample '😎' icon!"
console.log("Original String:", example)
// Original String: Hello, this is a sample '😎' icon!
const compressed = zip_encode(example)
console.log("Compressed Data:", compressed)
// Compressed Data: 80jNyclXNXJWNTIoycgsBlJgIhGIixNzC3JSgQx1VTcDVUs3VUsLVQtXdZCS5Pw8RQA=
const example_decompressed = zip_decode(compressed)
console.log("Decompressed String:", example_decompressed)
// Decompressed String: Hello, this is a sample '😎' icon!
<script src="https://unpkg.com/[email protected]"></script>
Upvotes: 4
Reputation: 6580
I would suggest you use a dedicated Web Worker for this. There is a pretty good explanation on Using Web Worker on the mozilla developers page.
Upvotes: 0
Reputation: 3241
lz-string looks like it will work.
var string = "2.545,3.552,8.568;2.553,9.898,6.542";
alert("Size of sample is: " + string.length);
var compressed = LZString.compress(string);
alert("Size of compressed sample is: " + compressed.length);
string = LZString.decompress(compressed);
alert("Sample is: " + string);
<script src="https://cdn.jsdelivr.net/gh/pieroxy/lz-string/libs/lz-string.js"></script>
Upvotes: 7