1.0.7 • Published 1 year ago

packbytes v1.0.7

Weekly downloads
-
License
-
Repository
github
Last release
1 year ago

Install:

npm i packbytes

Schema:

  • Define the structure of your data with a Schema
  • It will optimize and execute all low-level operations for you
  • Creates a smaller encoding faster and easier than any other schema-based system:
// example schema with all data types:

import { bool, bits, string, array, float, blob, schemas, PackBytes } from 'packbytes';

const schema = {
   a: bool,
   b: bits(1),
   c: bits(7),
   d: bits(25),
   e: string,
   x: string('str1', 'str2'),
   y: array(bits(5)),
   z: array({
      a: float(32),
      b: float(64),
      c: blob,
      d: blob(12),
      e: array(blob),
      f: array(string),
      1: array(string('str1', 'str2')),
      2: array(string('str1', 'str2')).size(3),
      3: array(array(bits(7))),
      4: schemas({ name1: bool, name2: array(bits(3)).size(2) }),
      5: array(schemas({ s1: string, s2: { field1: bool, field2: array(string('str1', 'str2')) } }))
   })
};

Example:

  • This code runs in both Node.js and Web Browser:

Schema:

import { bool, bits, array, PackBytes } from 'packbytes';

const schema = array({
   a: bool,
   b: bits(2),
   c: bits(5)
});

const encoder = new PackBytes(schema);

Encode:

const data = [
   { a: false, b: 0, c: 0 },
   { a: true, b: 1, c: 12 },
   { a: true, b: 3, c: 31 }
];

const buf = encoder.encode(data);

// buf.length == 3, encoded to 3 bytes, 24x smaller than JSON.stringify(data) at 73 bytes

sendOverNetwork(buf);
saveToDisk(buf);

Decode:

const data = encoder.decode(buf);

console.log(data);
// [
//    { a: false, b: 0, c: 0 },
//    { a: true, b: 1, c: 12 },
//    { a: true, b: 3, c: 31 }
// ]

Benchmark:

  • The benchmark encodes data 50x smaller than JSON and is 5x faster, also compare with other encoding methods:
Encodingtime (ns)bytes
packBytes551
packBytes_schema1551
avsc2704
protobuf2508
msgpack117033
json80053
  • Same benchmark with different data set, 25x smaller than JSON and 5x faster:
Encodingtime (ns)bytes
packBytes604
packBytes_schema2104
avsc3009
protobuf49017
msgpack145033
json1150102

API:

  • Detailed API description and user guide:
// all available exports:
import { bool, bits, string, array, float, blob, schemas, PackBytes } from 'packbytes';

// create a schema using any combination or nesting of schema types:
schema = bool // true or false
schema = bits(x) // x number bits for unsigned integer (max integer = 2**x-1)
schema = string // string of any length
schema = string('str1', 'str2', ..) // any of specific strings, auto-maps to integer
schema = float(x) // 32 or 64 bit floating point number
schema = blob // any length buffer
schema = blob(x) // specific byte size buffer 
schema = array(schema) // array of any schema type
schema = array(schema).size(x) // specific length array
schema = { field1: schema, field2: schema, .. } // object with multiple fields
schema = schemas({ schema1: schema, schema2: schema, .. }) // multiple schemas mapped to 1 schema

// create encoder by providing schema:
// accepts schema object or JSON.stringify(schema) string for easy transfer from server to client:
encoder = new PackBytes(schema)

buf = encoder.encode(data) // encode data
buf = encoder.encode(schema_name, data) // encode data with specific schema from 'schemas' type

data = encoder.decode(buf) // decode, returns orginal data
1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago