serialize-comparison

3.0.2 • Public • Published

Logo serialize-comparison

MIT license Travis build npm package Website Donate button

I was looking for a quick and efficient method of compressing a JavaScript object into a url-safe (actually url-fragment-safe, which seems to allow 77 different characters) string. This was needed to convert a React component state into a permalink (like example.com/#setStateWithUrlFragmentData), although React component states are just plain JavaScript objects, so this project is not framework-depended.

The comparison base currently contains over 160 different methods of compressing objects into strings and corresponding scripts to test those methods regarding performance and compression rate.

You can find the latest test results over here: serialize-comparison.jaid.codes

Winner

The most efficient way of compressing objects turned out to be json5 as serializer and brotli as compression algorithm. The Brotli compressor performs pretty fast and gives insane compression results for a wide spectrum of input data.

Example:

import json5 from "json5"
import { compressSync } from "iltorb"
 
const data = {a: "a", b: 2}
const jsonBin = Buffer.from(json5.stringify(data))
const compressed = Buffer.from(compressSync(jsonBin)).toString("base64")
// compressed = "CwWAe2E6J2EnLGI6Mn0D"

The search is not over

If you built or know another good serializer or compression algorithm, feel free to suggest it in an issue!

Command Line

The CLI script runs the tests locally and prints the top 10 (regarding compression ratio) as an ansi-colored table.

npx:

npx serialize-comparison

Yarn:

yarn global add serialize-comparison
serialize-comparison

npm:

npm install --global serialize-comparison
serialize-comparison

CLI Screenshot

License

Copyright © 2019 Licensed under MIT Jaid (github.com/Jaid)

Package Sidebar

Install

npm i serialize-comparison

Weekly Downloads

0

Version

3.0.2

License

MIT

Unpacked Size

15.9 kB

Total Files

12

Last publish

Collaborators

  • jaid