Last active
October 24, 2021 14:13
-
-
Save uu64/93c5b99be2629c7be54f3c353253ae20 to your computer and use it in GitHub Desktop.
Comparison of string compression algorithms in nodejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
const { Buffer } = require("buffer") | |
const { | |
brotliCompressSync, | |
deflateSync, | |
gzipSync, | |
} = require("zlib") | |
const toBase64 = (str) => { | |
return Buffer.from(str).toString("base64url") | |
} | |
const compressDeflate = (str) => { | |
const compressed = deflateSync(str).toString("base64url") | |
return compressed | |
} | |
const compressGzip = (str) => { | |
const compressed = gzipSync(str).toString("base64url") | |
return compressed | |
} | |
const compressBrotli = (str) => { | |
const compressed = brotliCompressSync(str).toString("base64url") | |
return compressed | |
} | |
const print = (str) => { | |
console.log(`data: ${str}, len: ${str.length}`) | |
const base64 = toBase64(str) | |
console.log(`to base64url: ${base64}, len: ${base64.length}`) | |
const deflate = compressDeflate(str) | |
console.log(`deflate and to base64url: ${deflate}, len: ${deflate.length}`) | |
const gzip = compressGzip(str) | |
console.log(`gzip and to base64url: ${gzip}, len: ${gzip.length}`) | |
const brotli = compressBrotli(str) | |
console.log(`brotli and to base64url: ${brotli}, len: ${brotli.length}`) | |
console.log() | |
} | |
const id = "abcdefg_123x456_123y123z123c" | |
const obj = { | |
id: id, | |
name: "John Smith", | |
age: 30, | |
address: "Tokyo, Japan", | |
} | |
print(id) | |
print(JSON.stringify(obj)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The result is the following.