Skip to content

Instantly share code, notes, and snippets.

@174n
Last active April 3, 2021 13:34
Show Gist options
  • Save 174n/b908ca9c13400b99e93bb4297e822b0a to your computer and use it in GitHub Desktop.
Save 174n/b908ca9c13400b99e93bb4297e822b0a to your computer and use it in GitHub Desktop.
JSON compression benchmark
const jsonpack = require("jsonpack");
const lzwcompress = require("lzwcompress");
const cjson = require('compressed-json');
const base2048 = require("base2048");
const Table = require('cli-table');
const data = require("./data.json");
const base64string = data => Buffer.from(data).toString("base64");
const getSize = data => Buffer.byteLength(JSON.stringify(data), 'utf8');
const results = [];
results.push(["original", getSize(data)]);
results.push(["jsonpack", getSize({d:jsonpack.pack(data)})]);
results.push(["cjson", getSize(cjson.compress(data))]);
results.push(["lzwcompress", getSize({d:base64string(lzwcompress.pack(data))})]);
results.push(["lzwcompress+jsonpack", getSize({d:base64string(lzwcompress.pack(jsonpack.pack(data)))})]);
results.push(["lzwcompress+cjson", getSize({d:base64string(lzwcompress.pack(cjson.compress(data)))})]);
results.push(["lzwcompress+cjson+base2048", getSize({d:base2048.encode(lzwcompress.pack(cjson.compress(data)))})]);
results.push(["cjson + jsonpack", getSize({d:jsonpack.pack(cjson.compress(data))})]);
const colWidths = [
results.reduce((a, r) => a > r[0].length ? a : r[0].length, "compression".length) + 2,
results.reduce((a, r) => a > (""+r[1]).length ? a : (""+r[1]).length, "size".length) + 2
];
const table = new Table({
head: ['Compression', 'Size'],
colWidths
});
results.sort((a, b) => a[1] - b[1]).forEach(o => table.push(o))
console.log(table.toString());
{
"dependencies": {
"base2048": "^1.0.3",
"cli-table": "^0.3.6",
"compressed-json": "^1.0.16",
"jsonpack": "^1.1.5",
"lzwcompress": "^0.7.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment