Codec → Compress
Compress
Compress a string with LZ77 or LZH (Deflate), and check whether a buffer is already compressed before compressing again.
Algorithms
| Algorithm | Access | Notes |
|---|---|---|
| LZ77 | Codec.LZ77 | Sliding window. Fast, good ratio on repetitive data. |
| LZH / Deflate | Codec.LZH | LZ77 + Huffman coding. Better ratio, slightly more CPU. Added in v2.3. |
API
Codec.LZ77:Compress(data: string) → string
Codec.LZH:Compress(data: string) → string
Codec:IsCompressed(data: string) → boolean
Manual compression
local Codec = RoExpress.Codec
local json = game:GetService("HttpService"):JSONEncode(bigTable)
local packed = Codec.LZH:Compress(json)
print(#json, "→", #packed) -- original vs compressed size
Checking before compress
Use Codec:IsCompressed to avoid double-compressing a buffer that was already compressed elsewhere:
if not Codec:IsCompressed(data) then
data = Codec.LZH:Compress(data)
end
Manual vs route opt-in
Manual compression is useful when you need fine-grained control — for example, compressing data before storing it, or compressing only part of a payload. For route-level compression handled automatically by App, see Route Integration.
Size advice
| Scenario | Recommendation |
|---|---|
| Large repeated-structure data (map tiles, inventories) | LZH — better ratio |
| High-frequency small updates | Don't compress — overhead not worth it |
| Binary buffer data | Use Stream instead |
Compression only helps when the data has enough repetition to exploit. Always log
#original vs #compressed during development to verify you're actually saving bytes.See also
← Codec · Decompress | restore a compressed buffer · Route Integration | compress = true on routes