Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

compress-json

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

compress-json - npm Package Compare versions

Comparing version 0.0.0 to 0.1.0

27

dist/core.js

@@ -7,5 +7,5 @@ "use strict";

function compress(o) {
let mem = memory_1.makeInMemoryMemory();
let root = memory_1.addValue(mem, o, undefined);
let values = memory_1.memToValues(mem);
const mem = memory_1.makeInMemoryMemory();
const root = memory_1.addValue(mem, o, undefined);
const values = memory_1.memToValues(mem);
return [values, root];

@@ -18,5 +18,5 @@ }

}
let o = {};
let vs = s.split('|');
let n = vs.length;
const o = {};
const vs = s.split('|');
const n = vs.length;
for (let i = 1; i < n; i += 2) {

@@ -35,5 +35,5 @@ let k = vs[i];

}
let vs = s.split('|');
let n = vs.length - 1;
let xs = new Array(n);
const vs = s.split('|');
const n = vs.length - 1;
const xs = new Array(n);
for (let i = 0; i < n; i++) {

@@ -47,4 +47,7 @@ let v = vs[i + 1];

function decode(values, key) {
let id = encode_1.decodeKey(key);
let v = values[id];
if (key === '') {
return null;
}
const id = encode_1.decodeKey(key);
const v = values[id];
if (v === null) {

@@ -59,3 +62,3 @@ return v;

case 'string':
let prefix = v[0] + v[1];
const prefix = v[0] + v[1];
switch (prefix) {

@@ -62,0 +65,0 @@ case 'b|':

@@ -5,3 +5,3 @@ "use strict";

function encodeNum(num) {
let a = 'n|' + number_1.num_to_s(num);
const a = 'n|' + number_1.num_to_s(num);
return a;

@@ -18,5 +18,3 @@ // let b = num.toString()

function decodeKey(key) {
return typeof key === 'number'
? key
: number_1.s_to_int(key);
return typeof key === 'number' ? key : number_1.s_to_int(key);
}

@@ -26,5 +24,3 @@ exports.decodeKey = decodeKey;

// return 'b|' + bool_to_s(b)
return b
? 'b|T'
: 'b|F';
return b ? 'b|T' : 'b|F';
}

@@ -43,3 +39,3 @@ exports.encodeBool = encodeBool;

function encodeStr(str) {
let prefix = str[0] + str[1];
const prefix = str[0] + str[1];
switch (prefix) {

@@ -46,0 +42,0 @@ case 'b|':

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var core_1 = require("./core");
exports.compress = core_1.compress;
exports.decompress = core_1.decompress;
//# sourceMappingURL=index.js.map

@@ -7,6 +7,6 @@ "use strict";

function memToValues(mem) {
let n = mem.keyCount;
let vs = new Array(n);
const n = mem.keyCount;
const vs = new Array(n);
for (let i = 0; i < n; i++) {
let v = mem.store.get(i.toString());
const v = mem.store.get(i.toString());
vs[i] = v;

@@ -18,3 +18,3 @@ }

function makeInMemoryStore() {
let mem = {};
const mem = {};
return {

@@ -25,3 +25,3 @@ get(key) {

forEach(cb) {
for (let [key, value] of Object.entries(mem)) {
for (const [key, value] of Object.entries(mem)) {
if (cb(key, value) === 'break') {

@@ -53,4 +53,4 @@ return;

}
let id = mem.keyCount++;
let key = number_1.num_to_s(id);
const id = mem.keyCount++;
const key = number_1.num_to_s(id);
mem.store.set(id.toString(), value);

@@ -61,2 +61,5 @@ mem.cache.set(value, key);

function addValue(mem, o, parent) {
if (o === null) {
return '';
}
switch (typeof o) {

@@ -75,3 +78,3 @@ case 'undefined':

for (let i = 0; i < o.length; i++) {
let v = o[i];
const v = o[i];
acc += '|' + addValue(mem, v, o);

@@ -87,4 +90,4 @@ }

Object.entries(o).forEach(([key, value]) => {
let k = addValue(mem, key, o);
let v = addValue(mem, value, o);
const k = addValue(mem, key, o);
const v = addValue(mem, value, o);
acc += '|' + k + '|' + v;

@@ -91,0 +94,0 @@ });

@@ -5,2 +5,1 @@ export declare function s_to_int(s: string): number;

export declare function s_to_num(s: string): number;
export declare function test(): void;

@@ -5,17 +5,17 @@ "use strict";

for (let i = 0; i < 10; i++) {
let c = String.fromCharCode(48 + i);
const c = String.fromCharCode(48 + i);
i_to_s += c;
}
for (let i = 0; i < 26; i++) {
let c = String.fromCharCode(65 + i);
const c = String.fromCharCode(65 + i);
i_to_s += c;
}
for (let i = 0; i < 26; i++) {
let c = String.fromCharCode(65 + 32 + i);
const c = String.fromCharCode(65 + 32 + i);
i_to_s += c;
}
let N = i_to_s.length;
let s_to_i = {};
const N = i_to_s.length;
const s_to_i = {};
for (let i = 0; i < N; i++) {
let s = i_to_s[i];
const s = i_to_s[i];
s_to_i[s] = i;

@@ -27,3 +27,3 @@ }

for (let i = s.length - 1; i >= 0; i--) {
let c = s[i];
const c = s[i];
let x = s_to_i[c];

@@ -41,6 +41,6 @@ x *= pow;

}
let acc = [];
const acc = [];
while (int !== 0) {
let i = int % N;
let c = i_to_s[i];
const i = int % N;
const c = i_to_s[i];
acc.push(c);

@@ -78,21 +78,2 @@ int -= i;

exports.s_to_num = s_to_num;
function test() {
let xs = [
1234567890,
987654321,
1234.4321,
];
for (let x of xs) {
let s = num_to_s(x);
let y = s_to_num(s);
if (x !== y) {
console.error({ x, s, y });
throw new Error('incorrect encode/decode');
}
console.log(x, '->', s);
}
console.log('pass:', __filename.replace(__dirname + '/', ''));
}
exports.test = test;
// test()
//# sourceMappingURL=number.js.map
{
"name": "compress-json",
"version": "0.0.0",
"description": "",
"version": "0.1.0",
"description": "convert JSON data to space efficient format",
"keywords": [],

@@ -36,10 +36,9 @@ "author": {

],
"dependencies": {
"dependencies": {},
"devDependencies": {
"@beenotung/tslib": "^16.20.1",
"tslib": "^1.11.2"
},
"devDependencies": {
"@types/node": "*",
"compressed-json": "^1.0.15",
"husky": "^4.2.5",
"jsonpack": "^1.1.5",
"prettier": "^2.0.5",

@@ -46,0 +45,0 @@ "rimraf": "^3.0.2",

@@ -5,12 +5,171 @@ # compress-json

Inspired by compressed-json and jsonpack
[![npm Package Version](https://img.shields.io/npm/v/compress-json.svg?maxAge=2592000)](https://www.npmjs.com/package/compress-json)
Inspired by [compressed-json](https://github.com/okunishinishi/node-compressed-json) and [jsonpack](https://github.com/rgcl/jsonpack).
This library is optimized to reduce represent json object in compact format, which can save network bandwidth and disk space.
It is not optimized for writing nor querying throughput.
Although the reduced IO may speed up usage of lmdb on frequently redundant data, that is not the design goal.
## Features
- Object key order is preserved
- Repeated values are stored only once
- Numbers are encoded in base62 format (0-9A-Za-z)
- Support multiple storage backend
- in-memory object / array / Map
- localStorage
- lmdb
- leveldb (sync mode)
- custom adapter
## Installation
```bash
npm i -S compress-json
```
## Usage
```typescript
import { compress, decompress } from 'compress-json'
let data = {
user: 'Alice',
// more fields of any json values (string, number, array, object, e.t.c.)
}
let compressed = compress(data) // the result is an array
fetch('/submit', {
method: 'post',
body: JSON.stringify(compressed) // convert into string if needed
})
let reversed = decompress(compressed)
data === reversed // will be false
JSON.stringify(data) === JSON.stringify(reversed) // will be true
```
## Format
**Sample data**:
```typescript
let data = {
int: 42,
float: 12.34,
str: 'Alice',
longStr: 'A very very long string, that is repeated',
longNum: 9876543210.123455,
bool: true,
arr: [
42,
12.34,
'Alice',
true,
false,
'A very very long string, that is repeated',
9876543210.123455
],
obj: {
id: 123,
name: 'Alice',
role: [ 'Admin', 'User', 'Guest' ],
longStr: 'A very very long string, that is repeated',
longNum: 9876543210.123455
},
escape: [ 's|str', 'n|123', 'o|1', 'a|1', 'b|T', 'b|F' ]
}
```
**Compressed data**:
```typescript
// [ encoded value array, root value index ]
let compressed = [
[ // encoded value array
'int', // string
'n|g', // number (integer) (base62-encoded)
'float',
'n|C.h', // number (float) (integer part and decimals are base62-encoded separately)
'str',
'Alice',
'longStr',
'A very very long string, that is repeated',
'longNum',
'n|AmOy42.2KCf',
'bool',
'b|T', // boolean (true)
'arr',
'b|F', // boolean (false)
'a|1|3|5|B|D|7|9', // array
'obj',
'id',
'n|1z',
'name',
'role',
'Admin',
'User',
'Guest',
'a|K|L|M',
'o|G|H|I|5|J|N|6|7|8|9', // object
'escape',
's|s|str', // escaped string
's|n|123', // escaped number
's|o|1',
's|a|1',
's|b|T', // escaped boolean
's|b|F',
'a|Q|R|S|T|U|V',
'o|0|1|2|3|4|5|6|7|8|9|A|B|C|E|F|O|P|W'
],
'X' // root value index
]
```
## Benchmark
| raw json* | compress-json (this library) | compressed-json | jsonpack |
|---|---|---|---|
| a | a | a | a |
Test file: [compress-test.ts](./test/compress-test.ts)
raw json*: in the format of `JSON.stringify()`, without indentation
Sample data in use: json data of 109,164 threads and 724,905 post crawled from a discuz forum. Truncated in varies of size for testing.
Algorithms in comparison:
- JSON (`JSON.stringify` without indentation)
- compressed-json
- jsonpack
- compress-json (this library)
(Binary compression algorithm is not considered)
### Compressed Size
| sample | JSON | compressed-json | jsonpack | **compress-json** |
|---|---|---|---|---|
| all | 263M | 199M | - | 180M |
| 100,000 | 235M | 178M | - | 162M |
| 50,000 | 70M | 55M | - | 51M |
| 10,000 | 34M | 26M | - | 24M |
| 2,000 | 6.6M | 5.0M | 5.3M | 4.7M |
| 1,000 | 4.8M | 3.7M | 3.8M | 3.5M |
| 100 | 335K | 265K | 271K | 251K |
| 10 | 4.0K | 3.3K | 3.0K | 3.4K |
### Compression Time
| sample | JSON | compressed-json | jsonpack | **compress-json** |
|---|---|---|---|---|
| all | 1,654ms | 12,674ms | timeout* | 20,208ms |
| 100,000 | 1,500ms | 10,921ms | timeout* | 16,773ms |
| 50,000 | 462ms | 3,047ms | timeout* | 5,127ms |
| 10,000 | 146ms | 1,278ms | timeout* | 2,097ms |
| 2,000 | 35ms | 328ms | 21,018ms | 520ms |
| 1,000 | 20ms | 270ms | 12,960ms | 487ms |
| 100 | 1ms | 18ms | 47ms | 35ms |
| 10 | 0.3ms | 1.8ms | 1.6ms | 2.4ms |
*timeout: excess 1 minute
### Decompress Time
| sample | JSON | compressed-json | jsonpack | **compress-json** |
|---|---|---|---|---|
| all | 1,908ms | 4,611ms | - | 9,085ms |
| 100,000 | 1,744ms | 3,740ms | - | 7,707ms |
| 50,000 | 558ms | 1,066ms | - | 2,900ms |
| 10,000 | 173ms | 460ms | - | 1,144ms |
| 2,000 | 47ms | 108ms | 189ms | 347ms |
| 1,000 | 34ms | 90ms | 160ms | 297ms |
| 100 | 8ms | 11ms | 16ms | 22ms |
| 10 | 2.9ms | 2.1ms | 1.1ms | 1.2ms |

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc