I need to store some very large preset arrays in a javascript file. Each array needs to be compressed separately and the javascript file needs to be as small as possible. One of those arrays needs to be unpacked very quickly at run time (when the user chooses which array is needed).
The problem is the Visual Studio Code complains about carriage returns and lines not being ended correctly. This is due to the lzw compression.
Compression is needed because the original array appears about 600KB, the JSON string appears about 30KB and the final compressed string is just 14KB.
I do not want to use a database, as the call and return for that would in itself put notable delays when that array is called. It has to be fast (it is critical to the program).
So, I used JSON to make the array into a string. Then I used lzw compression to compress that string. That string (and the other strings of similarly compressed arrays) are then put into the javascript file.
(NOTE: In the following code, the array is just a small fraction of the real array which is about 32 times larger.)
function lzw_encode(s) {
let i, dict = {}, data = (s + "").split(""),
out = [], currChar, phrase = data[0], code = 256;
for (i = 1, len = data.length; i < len; i++) {
currChar = data[i];
if (dict[phrase + currChar] != null) {
phrase += currChar;
}
else {
out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0));
dict[phrase + currChar] = code;
code++;
phrase = currChar;
}
}
out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0));
for (i = 0, len = out.length; i < len; i++) {
out[i] = String.fromCharCode(out[i]);
}
return out.join("");
}
let a = [
[
[
[
[
[
770,
784,
795,
],
[
804,
816,
830,
],
[
770,
784,
795,
],
],
[
[
672,
695,
721,
],
[
738,
750,
761,
],
[
672,
695,
721,
],
],
],
[
[
[
672,
695,
721,
],
[
738,
750,
761,
],
[
672,
695,
721,
],
],
[
[
555,
572,
592,
],
[
606,
626,
649,
],
[
555,
572,
592,
],
],
],
[
[
[
744,
755,
764,
],
[
770,
784,
795,
],
[
744,
755,
764,
],
],
[
[
612,
635,
655,
],
[
672,
695,
721,
],
[
612,
635,
655,
],
],
],
],
[
[
[
[
655,
678,
704,
],
[
655,
678,
704,
],
[
738,
750,
761,
],
],
[
[
540,
560,
577,
],
[
540,
560,
577,
],
[
606,
626,
649,
],
],
],
[
[
[
540,
560,
577,
],
[
540,
560,
577,
],
[
606,
626,
649,
],
],
[
[
454,
468,
480,
],
[
454,
468,
480,
],
[
503,
520,
534,
],
],
],
[
[
[
598,
618,
641,
],
[
598,
618,
641,
],
[
672,
695,
721,
],
],
[
[
497,
514,
529,
],
[
497,
514,
529,
],
[
555,
572,
592,
],
],
],
],
],
[
[
[
[
[
638,
652,
663,
],
[
672,
684,
698,
],
[
638,
652,
663,
],
],
[
[
540,
563,
589,
],
[
606,
618,
629,
],
[
540,
563,
589,
],
],
],
[
[
[
540,
563,
589,
],
[
606,
618,
629,
],
[
540,
563,
589,
],
],
[
[
423,
440,
460,
],
[
474,
494,
517,
],
[
423,
440,
460,
],
],
],
[
[
[
612,
623,
632,
],
[
638,
652,
663,
],
[
612,
623,
632,
],
],
[
[
480,
503,
523,
],
[
540,
563,
589,
],
[
480,
503,
523,
],
],
],
],
[
[
[
[
523,
546,
572,
],
[
523,
546,
572,
],
[
606,
618,
629,
],
],
[
[
408,
428,
445,
],
[
408,
428,
445,
],
[
474,
494,
517,
],
],
],
[
[
[
408,
428,
445,
],
[
408,
428,
445,
],
[
474,
494,
517,
],
],
[
[
322,
336,
348,
],
[
322,
336,
348,
],
[
371,
388,
402,
],
],
],
[
[
[
466,
486,
509,
],
[
466,
486,
509,
],
[
540,
563,
589,
],
],
[
[
365,
382,
397,
],
[
365,
382,
397,
],
[
423,
440,
460,
],
],
],
],
],
[
[
[
[
[
551,
565,
576,
],
[
585,
597,
611,
],
[
551,
565,
576,
],
],
[
[
496,
505,
513,
],
[
522,
531,
542,
],
[
496,
505,
513,
],
],
],
[
[
[
496,
505,
513,
],
[
522,
531,
542,
],
[
496,
505,
513,
],
],
[
[
456,
462,
467,
],
[
473,
482,
487,
],
[
456,
462,
467,
],
],
],
[
[
[
525,
536,
545,
],
[
551,
565,
576,
],
[
525,
536,
545,
],
],
[
[
476,
485,
490,
],
[
496,
505,
513,
],
[
476,
485,
490,
],
],
],
],
[
[
[
[
490,
499,
508,
],
[
490,
499,
508,
],
[
522,
531,
542,
],
],
[
[
450,
456,
465,
],
[
450,
456,
465,
],
[
473,
482,
487,
],
],
],
[
[
[
450,
456,
465,
],
[
450,
456,
465,
],
[
473,
482,
487,
],
],
[
[
419,
424,
430,
],
[
419,
424,
430,
],
[
439,
444,
450,
],
],
],
[
[
[
470,
479,
485,
],
[
470,
479,
485,
],
[
496,
505,
513,
],
],
[
[
436,
442,
447,
],
[
436,
442,
447,
],
[
456,
462,
467,
],
],
],
],
],
[
[
[
[
[
462,
476,
487,
],
[
496,
508,
522,
],
[
462,
476,
487,
],
],
[
[
407,
416,
424,
],
[
433,
442,
453,
],
[
407,
416,
424,
],
],
],
[
[
[
407,
416,
424,
],
[
433,
442,
453,
],
[
407,
416,
424,
],
],
[
[
367,
373,
378,
],
[
384,
393,
398,
],
[
367,
373,
378,
],
],
],
[
[
[
436,
447,
456,
],
[
462,
476,
487,
],
[
436,
447,
456,
],
],
[
[
387,
396,
401,
],
[
407,
416,
424,
],
[
387,
396,
401,
],
],
],
],
[
[
[
[
401,
410,
419,
],
[
401,
410,
419,
],
[
433,
442,
453,
],
],
[
[
361,
367,
376,
],
[
361,
367,
376,
],
[
384,
393,
398,
],
],
],
[
[
[
361,
367,
376,
],
[
361,
367,
376,
],
[
384,
393,
398,
],
],
[
[
330,
335,
341,
],
[
330,
335,
341,
],
[
350,
355,
361,
],
],
],
[
[
[
381,
390,
396,
],
[
381,
390,
396,
],
[
407,
416,
424,
],
],
[
[
347,
353,
358,
],
[
347,
353,
358,
],
[
367,
373,
378,
],
],
],
],
],
];
let str = JSON.stringify(a);
let strComp = lzw_encode(str);
document.getElementById("lab").innerHTML = strComp;
<label id="lab"></label>
The compressed string starting “[Āā770,784Ć95],….” is the genuine result. I then unpack this at run time when the array is called by doing the reverse. Its all FAST, so it does what it is meant to do.
This appears to work in production, so is not a major issue.
BUT, is there another FAST compression and unpacking that does not cause problems with carriage returns etc?
I wrote my own with just a standard character set, but it was a lot slower and did not compress as much.