Skip to content

Commit

Permalink
Merge pull request #10 from compactr/v2.2.0
Browse files Browse the repository at this point in the history
V2.2.0
  • Loading branch information
fed135 authored Oct 6, 2017
2 parents 81c6f9d + f33e6a6 commit e115eda
Show file tree
Hide file tree
Showing 17 changed files with 177 additions and 136 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: node_js
node_js:
- "8.1"
- "6"
- "8"
script: "npm run test && npm run bench"
cache: yarn
39 changes: 14 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@
<br/>

[![Compactr](https://img.shields.io/npm/v/compactr.svg)](https://www.npmjs.com/package/compactr)
[![Node](https://img.shields.io/badge/node->%3D4.0-blue.svg)](https://nodejs.org)
[![Node](https://img.shields.io/badge/node->%3D6.0-blue.svg)](https://nodejs.org)
[![Build Status](https://travis-ci.org/compactr/compactr.js.svg?branch=master)](https://travis-ci.org/compactr/compactr.js)
[![Gitter](https://img.shields.io/gitter/room/compactr/compactr.svg)](https://gitter.im/compactr/compactr)

---

## What is this and why does it matter?

Protocol Buffers are awesome. Having schemas to deflate and inflate data while maintaining some kind of validation is a great concept. Compactr's goal is to build on that to better suit Node development and reduce repetition by allowing you to build schemas for your data directly in your scripting language. For example, if you have a DB schema for a model, you could use that directly as a schema for Compactr.
Protocol Buffers are awesome. Having schemas to deflate and inflate data while maintaining some kind of validation is a great concept. Compactr's goal is to build on that to better suit the Javascript ecosystem.

[More](docs/ABOUT.md)


## Install
Expand All @@ -47,51 +49,38 @@ const userSchema = Compactr.schema({
// Encoding
userSchema.write({ id: 123, name: 'John' });

// Get the schema header bytes (for partial loads)
// Get the header bytes
const header = userSchema.headerBytes();

// Get the partial load bytes
// Get the content bytes
const partial = userSchema.contentBytes();

// Get the full header + content bytes
// Get the full payload (header + content bytes)
const buffer = userSchema.bytes();




// Decoding (full)
// Decoding a full payload
const content = userSchema.read(buffer);

// Decoding (partial)
// Decoding a partial payload (content)
const content = userSchema.readContent(partial);
```


## Performances

```
[Array] JSON x 188 ops/sec ±2.47% (73 runs sampled)
[Array] Compactr x 248 ops/sec ±3.16% (72 runs sampled)
## Speed comparison

[Boolean] JSON x 220 ops/sec ±5.04% (71 runs sampled)
[Boolean] Compactr x 731 ops/sec ±7.57% (74 runs sampled)
![Speed](http://res.cloudinary.com/kalm/image/upload/v1507323565/compactr_speed_adhlsk.png)

[Float] JSON x 159 ops/sec ±3.41% (70 runs sampled)
[Float] Compactr x 476 ops/sec ±1.58% (85 runs sampled)
*Measured against plain JSON serialization + convertion to buffer. Compactr serialization is performed with default settings via the partial (content only) load method*

[Integer] JSON x 264 ops/sec ±1.79% (79 runs sampled)
[Integer] Compactr x 885 ops/sec ±1.36% (84 runs sampled)
[Object] JSON x 139 ops/sec ±1.89% (76 runs sampled)
[Object] Compactr x 169 ops/sec ±1.52% (80 runs sampled)
[String] JSON x 107 ops/sec ±6.86% (64 runs sampled)
[String] Compactr x 167 ops/sec ±4.86% (72 runs sampled)
```


## Size comparison

![Size](http://res.cloudinary.com/kalm/image/upload/v1507323565/compactr_bytes_cbjxka.png)

JSON: `{"id":123,"name":"John"}`: 24 bytes

Compactr (full): `<Buffer 02 00 01 01 04 7b 4a 6f 68 6e>`: 10 bytes
Expand Down
4 changes: 2 additions & 2 deletions benchmarks/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function arrJSON() {
let packed, unpacked;

for(let i = 0; i<mult*mult; i++) {
packed = new Buffer(JSON.stringify({ id: i, arr: ['a', 'b', 'c'] }));
packed = Buffer.from(JSON.stringify({ id: i, arr: ['a', 'b', 'c'] }));
unpacked = JSON.parse(packed.toString());
}
}
Expand All @@ -39,7 +39,7 @@ function arrCompactr() {
let packed, unpacked;

for(let i = 0; i<mult*mult; i++) {
packed = User.write({ id: i, arr: ['a', 'b', 'c'] }).contentArray();
packed = User.write({ id: i, arr: ['a', 'b', 'c'] }).contentBuffer();
unpacked = User.readContent(packed);
}
}
4 changes: 2 additions & 2 deletions benchmarks/boolean.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function boolJSON() {
let packed, unpacked;

for(let i = 0; i<mult*mult; i++) {
packed = new Buffer(JSON.stringify({ id: i, bool: !!Math.random() }));
packed = Buffer.from(JSON.stringify({ id: i, bool: !!Math.random() }));
unpacked = JSON.parse(packed.toString());
}
}
Expand All @@ -39,7 +39,7 @@ function boolCompactr() {
let packed, unpacked;

for(let i = 0; i<mult*mult; i++) {
packed = User.write({ id: i, bool: !!Math.random() }).contentArray();
packed = User.write({ id: i, bool: !!Math.random() }).contentBuffer();
unpacked = User.readContent(packed);
}
}
40 changes: 20 additions & 20 deletions benchmarks/compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let User = Compactr.schema({
int: { type: 'double' }}
},
arr: { type: 'array', items: { type: 'string' }}
});
}, { keyOrder: true });

const str_test = 'abcdef';
const int_test = 97.5;
Expand Down Expand Up @@ -43,7 +43,7 @@ function strJSON() {
time = Date.now();

for(let i = 0; i<mult*mult; i++) {
packed = new Buffer(JSON.stringify({ id: i, str: str_test }));
packed = Buffer.from(JSON.stringify({ id: i, str: str_test }));
}

encodeTime = Date.now() - time;
Expand All @@ -52,7 +52,7 @@ function strJSON() {
time = Date.now();

for(let i = 0; i<mult*mult; i++) {
packed = new Buffer(JSON.stringify({ id: i, str: str_test }));
packed = Buffer.from(JSON.stringify({ id: i, str: str_test }));
unpacked = JSON.parse(packed.toString());
}

Expand All @@ -64,7 +64,7 @@ function strCompactr() {
time = Date.now();

for(let i = 0; i<mult*mult; i++) {
packed = User.write({ id: i, str: str_test }, { coerse }).array();
packed = User.write({ id: i, str: str_test }, { coerse }).buffer();
}

encodeTime = Date.now() - time;
Expand All @@ -73,7 +73,7 @@ function strCompactr() {
time = Date.now();

for(let i = 0; i<mult*mult; i++) {
packed = User.write({ id: i, str: str_test }, { coerse }).array();
packed = User.write({ str: str_test, id: i }, { coerse }).buffer();
unpacked = User.read(packed);
}

Expand All @@ -97,7 +97,7 @@ function intJSON() {
time = Date.now();

for(let i = 0; i<mult*mult; i++) {
packed = new Buffer(JSON.stringify({ id: i, int: int_test }));
packed = Buffer.from(JSON.stringify({ id: i, int: int_test }));
unpacked = JSON.parse(packed.toString());
}

Expand All @@ -109,7 +109,7 @@ function intCompactr() {
time = Date.now();

for(let i = 0; i<mult*mult; i++) {
packed = User.write({ id: i, int: int_test }, { coerse }).array();
packed = User.write({ id: i, int: int_test }, { coerse }).buffer();
}

encodeTime = Date.now() - time;
Expand All @@ -118,7 +118,7 @@ function intCompactr() {
time = Date.now();

for(let i = 0; i<mult*mult; i++) {
packed = User.write({ id: i, int: int_test }, { coerse }).array();
packed = User.write({ int: int_test, id: i }, { coerse }).buffer();
unpacked = User.read(packed);
}

Expand All @@ -130,15 +130,15 @@ function intCompactr() {
}

//strJSON();
//strCompactr();
strCompactr();
//intJSON();
//intCompactr();
intCompactr();

function objJSON() {
time = Date.now();

for(let i = 0; i<mult*mult; i++) {
packed = new Buffer(JSON.stringify({ id: i, obj: { int: int_test } }));
packed = Buffer.from(JSON.stringify({ id: i, obj: { int: int_test } }));
}

encodeTime = Date.now() - time;
Expand All @@ -147,7 +147,7 @@ function objJSON() {
time = Date.now();

for(let i = 0; i<mult*mult; i++) {
packed = new Buffer(JSON.stringify({ id: i, obj: { int: int_test } }));
packed = Buffer.from(JSON.stringify({ id: i, obj: { int: int_test } }));
unpacked = JSON.parse(packed.toString());
}

Expand All @@ -159,7 +159,7 @@ function objCompactr() {
time = Date.now();

for(let i = 0; i<mult*mult; i++) {
packed = User.write({ id: i, obj: { int: int_test } }).array();
packed = User.write({ id: i, obj: { int: int_test } }).buffer();
}

encodeTime = Date.now() - time;
Expand All @@ -168,7 +168,7 @@ function objCompactr() {
time = Date.now();

for(let i = 0; i<mult*mult; i++) {
packed = User.write({ id: i, obj: { int: int_test } }).array();
packed = User.write({ id: i, obj: { int: int_test } }).buffer();
unpacked = User.read(packed);
}

Expand All @@ -179,14 +179,14 @@ function objCompactr() {
console.log('obj size:', packed.length);
}

objJSON()
//objJSON()
objCompactr();

function arrJSON() {
time = Date.now();

for(let i = 0; i<mult*mult; i++) {
packed = new Buffer(JSON.stringify({ id: i, arr: ['a', 'b', 'c'] }));
packed = Buffer.from(JSON.stringify({ id: i, arr: ['a', 'b', 'c'] }));
}

encodeTime = Date.now() - time;
Expand All @@ -195,7 +195,7 @@ function arrJSON() {
time = Date.now();

for(let i = 0; i<mult*mult; i++) {
packed = new Buffer(JSON.stringify({ id: i, arr: ['a', 'b', 'c'] }));
packed = Buffer.from(JSON.stringify({ id: i, arr: ['a', 'b', 'c'] }));
unpacked = JSON.parse(packed.toString());
}

Expand All @@ -207,7 +207,7 @@ function arrCompactr() {
time = Date.now();

for(let i = 0; i<mult*mult; i++) {
packed = User.write({ id: i, arr: ['a', 'b', 'c'] }).array();
packed = User.write({ id: i, arr: ['a', 'b', 'c'] }).buffer();
}

encodeTime = Date.now() - time;
Expand All @@ -216,7 +216,7 @@ function arrCompactr() {
time = Date.now();

for(let i = 0; i<mult*mult; i++) {
packed = User.write({ id: i, arr: ['a', 'b', 'c'] }).array();
packed = User.write({ id: i, arr: ['a', 'b', 'c'] }).buffer();
unpacked = User.read(packed);
}

Expand All @@ -227,6 +227,6 @@ function arrCompactr() {
console.log('arr size:', packed.length);
}

arrJSON()
//arrJSON()
arrCompactr();
//schemaTest()
4 changes: 2 additions & 2 deletions benchmarks/double.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function floatJSON() {
let packed, unpacked;

for(let i = 0; i<mult*mult; i++) {
packed = new Buffer(JSON.stringify({ id: i, int: Math.random() }));
packed = Buffer.from(JSON.stringify({ id: i, int: Math.random() }));
unpacked = JSON.parse(packed.toString());
}
}
Expand All @@ -39,7 +39,7 @@ function floatCompactr() {
let packed, unpacked;

for(let i = 0; i<mult*mult; i++) {
packed = User.write({ id: i, int: Math.random() }).contentArray();
packed = User.write({ id: i, int: Math.random() }).contentBuffer();
unpacked = User.readContent(packed);
}
}
4 changes: 2 additions & 2 deletions benchmarks/integer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function intJSON() {
let packed, unpacked;

for(let i = 0; i<mult*mult; i++) {
packed = new Buffer(JSON.stringify({ id: i, int: Math.round(Math.random() * 1000000) }));
packed = Buffer.from(JSON.stringify({ id: i, int: Math.round(Math.random() * 1000000) }));
unpacked = JSON.parse(packed.toString());
}
}
Expand All @@ -39,7 +39,7 @@ function intCompactr() {
let packed, unpacked;

for(let i = 0; i<mult*mult; i++) {
packed = User.write({ id: i, int: Math.round(Math.random() * 1000000) }).contentArray();
packed = User.write({ id: i, int: Math.round(Math.random() * 1000000) }).contentBuffer();
unpacked = User.readContent(packed);
}
}
4 changes: 2 additions & 2 deletions benchmarks/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function objJSON() {
let packed, unpacked;

for(let i = 0; i<mult*mult; i++) {
packed = new Buffer(JSON.stringify({ id: i, obj: { str: '' + (Math.random()*0xffffff) } }));
packed = Buffer.from(JSON.stringify({ id: i, obj: { str: '' + (Math.random()*0xffffff) } }));
unpacked = JSON.parse(packed.toString());
}
}
Expand All @@ -40,7 +40,7 @@ function objCompactr() {
let packed, unpacked;

for(let i = 0; i<mult*mult; i++) {
packed = User.write({ id: i, obj: { str: '' + (Math.random()*0xffffff) } }).contentArray();
packed = User.write({ id: i, obj: { str: '' + (Math.random()*0xffffff) } }).contentBuffer();
unpacked = User.readContent(packed);
}
}
4 changes: 2 additions & 2 deletions benchmarks/string.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function strJSON() {
let packed, unpacked;

for(let i = 0; i<mult*mult; i++) {
packed = new Buffer(JSON.stringify({ id: i, str: '' + (Math.random()*0xffffff), special: String.fromCharCode(Math.random()*0xffff) }));
packed = Buffer.from(JSON.stringify({ id: i, str: '' + (Math.random()*0xffffff), special: String.fromCharCode(Math.random()*0xffff) }));
unpacked = JSON.parse(packed.toString());
}
}
Expand All @@ -41,7 +41,7 @@ function strCompactr() {
let packed, unpacked;

for(let i = 0; i<mult*mult; i++) {
packed = User.write({ id: i, str: '' + (Math.random()*0xffffff), special: String.fromCharCode(Math.random()*0xffff) }).contentArray();
packed = User.write({ id: i, str: '' + (Math.random()*0xffffff), special: String.fromCharCode(Math.random()*0xffff) }).contentBuffer();
unpacked = User.readContent(packed);
}
}
Loading

0 comments on commit e115eda

Please sign in to comment.