原文链接: lz-string 面向localstorage的字符串压缩库
https://www.npmjs.com/package/lz-string
这个库主要作用是将字符串压缩, 主要是为了能在localstorage中储存, 因为ls中只能存字符串, 而且有一些编码问题, 该库主要面向的是ls的字符串压缩和储存方案
pako 主要是字节数组, 而且压缩后的不知道能不能存到ls中, 主要是在一些奇怪的文字编码上, cocos用的lz, 所以还是尽量跟着他们用吧
对比如下
确实在压缩比上面lz占优势, 在小字符串上表现很好, 但是大字符串上pako时间上好很多
const pako = require("pako")
const fs = require("fs")
const LZ = require("lz-string")
function testPako(path) {
let s = fs.readFileSync(path, "utf8")
console.log("size:", s.length)
let st = +new Date()
let compressed = pako.deflate(s, { to: "string" })
let ed = +new Date()
console.log("pako", compressed.length, compressed.length / s.length, ed - st)
st = +new Date()
let raw = pako.inflate(compressed, { to: "string" })
ed = +new Date()
console.log("pako", raw.length, ed - st)
}
function testLZ(path) {
let s = fs.readFileSync(path, "utf8")
console.log("size:", s.length)
let st = +new Date()
let compressed = LZ.compress(s)
let ed = +new Date()
console.log("lz", compressed.length, compressed.length / s.length, ed - st)
st = +new Date()
let raw = LZ.decompress(compressed)
ed = +new Date()
console.log("lz", raw.length, ed - st)
}
let big = `D:/code/vue3/vue3-web-tools/public/worker.min.js`
let small = `D:/code/vue3/vue3-web-tools/public/tesseract-core.wasm.js`
testPako(big)
testPako(small)
testLZ(big)
testLZ(small)
/**
size: 100785
pako 31993 0.31743811082998463 232
pako 100785 46
size: 3880904
pako 1388523 0.35778339273530085 887
pako 3880904 272
size: 100785
lz 24197 0.24008533015825767 96
lz 100785 55
size: 3880904
lz 906636 0.23361464236167656 3319
lz 3880904 695
*/