jsonschema-key-compression
Advanced tools
Comparing version 1.0.1 to 1.0.2
{ | ||
"name": "jsonschema-key-compression", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Compress json-data based on it's json-schema", | ||
@@ -31,12 +31,17 @@ "author": "pubkey", | ||
"test:performance": "mocha -r ts-node/register --bail --exit ./test/performance.test.ts", | ||
"test:efficiency": "mocha -r ts-node/register --bail --exit ./test/efficiency.test.ts", | ||
"dev": "mocha -r ts-node/register test/unit/index.test.ts --watch --watch-extensions ts" | ||
}, | ||
"devDependencies": { | ||
"@types/faker": "4.1.5", | ||
"@types/mocha": "5.2.7", | ||
"@types/node": "12.0.10", | ||
"assert": "2.0.0", | ||
"async-test-util": "1.7.3", | ||
"faker": "4.1.0", | ||
"mocha": "6.1.4", | ||
"rimraf": "^2.6.3", | ||
"node-gzip": "1.1.2", | ||
"rimraf": "2.6.3", | ||
"ts-node": "8.3.0", | ||
"tslint": "5.17.0", | ||
"tslint": "5.18.0", | ||
"typescript": "3.5.2" | ||
@@ -43,0 +48,0 @@ }, |
# jsonschema-key-compression | ||
Compress json-data based on it's [json-schema](https://json-schema.org/) while still having valid json. | ||
Compress json-data based on its [json-schema](https://json-schema.org/) while still having valid json. | ||
It works by compressing long attribute-names into smaller ones and backwards. | ||
@@ -11,10 +11,21 @@ | ||
{ | ||
"firstName": "alice", | ||
"lastName": "wonder", | ||
"registerDate": "2019-06-01", | ||
"country": "de", | ||
"active": true, | ||
"eyeColor": "brown" | ||
"firstName": "Corrine", | ||
"lastName": "Ziemann", | ||
"title": "Ms.", | ||
"gender": "f", | ||
"zipCode": 75963, | ||
"countryCode": "en", | ||
"birthYear": 1960, | ||
"active": false, | ||
"shoppingCartItems": [ | ||
{ | ||
"productNumber": 29857, | ||
"amount": 1 | ||
}, | ||
{ | ||
"productNumber": 53409, | ||
"amount": 6 | ||
} | ||
] | ||
} | ||
``` | ||
@@ -26,15 +37,45 @@ | ||
{ | ||
"|a": "alice", | ||
"|b": "wonder", | ||
"|c": "2019-06-01", | ||
"|d": "de", | ||
"|e": true, | ||
"|f": "brown" | ||
"|e": "Corrine", | ||
"|g": "Ziemann", | ||
"|j": "Ms.", | ||
"|f": "f", | ||
"|k": 75963, | ||
"|d": "en", | ||
"|c": 1960, | ||
"|a": false, | ||
"|i": [ | ||
{ | ||
"|h": 29857, | ||
"|b": 1 | ||
}, | ||
{ | ||
"|h": 53409, | ||
"|b": 6 | ||
} | ||
] | ||
} | ||
``` | ||
The compressed version only needs **85 chars** while the non-compressed version needs **123 chars**. So by storing the compressed version, you can store up to 30% more data. | ||
## Efficiency | ||
The efficiency depends on the amount and length of the attribute names. | ||
* The uncompressed json-object from above has about **230 chars** as string | ||
* With the key-compression, this can be reduced to **140 chars** which saves about **40%** | ||
* Just using gzip on the json would result in **180 chars** | ||
* Using gzip+key-compression ends in a string with only **127 chars** | ||
You can reproduce these results by running `npm run test:efficiency`. | ||
## Performance | ||
The compression works pretty fast. Here are some time measurements on a single intel i7 CPU. | ||
* Creating a compression-table from the schema of the object above takes about `0.02ms` | ||
* Compressing the example-object from above takes about `0.021ms` | ||
* Decompressing takes about `0.027ms` per object | ||
You can reproduce these results by running `npm run test:performance`. | ||
## You should use this when | ||
- you want to save storage space in an NoSQL-database but still want to have valid json-data | ||
- you want to save storage space in an NoSQL-database but still want to have valid queryable json-data | ||
- you transmit many objects in many small requests over the network so that gzip cannot be efficient | ||
@@ -48,2 +89,8 @@ - you want to store json-data inside of the browser-storage (indexedDB or localstorage) and you reach the storage limit | ||
## Comparison with gzip | ||
Gzip generates its compression-flags [from the input](https://en.wikipedia.org/wiki/Gzip). This makes it more efficient, the more data is compressed at once. But gzip is less efficient the smaller the dataset is. | ||
The key-compression creates the compression-table from the jsonschema up front with has advantages when small pieces of data are compressed. | ||
## Usage | ||
@@ -68,3 +115,3 @@ | ||
### compressObject | ||
Compress a json-object based on it's schema. | ||
Compress a json-object based on its schema. | ||
@@ -75,3 +122,3 @@ ```js | ||
} from 'jsonschema-key-compression'; | ||
const compressedObject = createCompressionTable( | ||
const compressedObject = compressObject( | ||
compressionTable, | ||
@@ -91,3 +138,3 @@ jsonObject | ||
compressionTable, | ||
compressionTable | ||
compressedObject | ||
); | ||
@@ -97,3 +144,3 @@ ``` | ||
### compressedPath | ||
Transform a chain of json-attributes into it's compresed format. | ||
Transform a chain of json-attributes into its compressed format. | ||
@@ -120,3 +167,3 @@ ```js | ||
'|a.|b' // from compressedPath | ||
); | ||
); // > 'whateverNested.firstName' | ||
``` | ||
@@ -126,3 +173,3 @@ | ||
### compressQuery | ||
Compress a [mango-query](https://docs.mongodb.com/manual/tutorial/query-documents/) so that it can run over a NoSQL-Database that has stored compressed documents. | ||
Compress a [mango-query](https://docs.mongodb.com/manual/tutorial/query-documents/) so that it can run over a NoSQL-database that has stored compressed documents. | ||
@@ -133,3 +180,3 @@ ```js | ||
} from 'jsonschema-key-compression'; | ||
const decompressed = compressQuery( | ||
const compressed = compressQuery( | ||
compressionTable, | ||
@@ -145,4 +192,4 @@ { | ||
fields: [ | ||
'id', | ||
'name' | ||
'id', | ||
'name' | ||
], | ||
@@ -149,0 +196,0 @@ sort: [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
70394
36
1087
164
0
12