npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

js-binary

v1.2.0

Published

Encode/decode to a custom binary format, much more compact and faster than JSON/BSON

Downloads

3,893

Readme

JS Binary

Encode/decode to a custom binary format, much more compact and faster than JSON/BSON

Install

npm install js-binary

Goal

This module is analogous to JSON.stringify and JSON.parse, but instead of a JSON string, the data is encoded to a custom binary format (using a Buffer instance to store the data). This format was designed to be very compact and give support for more types (like Date and Buffer).

To reduce overhead in the format, it carries no information about types. This implies that you must define the data schema to encode/decode properly. Huge plus: this automatically validates the data against the given schema (input sanitization for free!). This binary format is well suited for very regular data, like API input/output.

Note that, since it's a binary format, it was not meant to be easily viewed/edited by hand.

Usage

var user = {
	name: {
		first: 'Guilherme',
		last: 'Souza'
	},
	pass: new Buffer('042697a30b2dafbdf91bf66bdacdcba8', 'hex'),
	creationDate: new Date('2014-04-11T21:22:32.504Z'),
	active: true,
	achievements: [3, 14, 15, 92, 65, 35]
}

var Type = require('js-binary').Type
var schema = new Type({
	name: {
		first: 'string',
		last: 'string'
	},
	pass: 'Buffer',
	creationDate: 'date',
	active: 'boolean',
	achievements: ['uint'],
	'optionalField?': 'int'
})

var encoded = schema.encode(user)

var decoded = schema.decode(encoded)

Benchmark

A quick example:

{
	"name": "js-binary",
	"published": "2014-12-21T23:42:46.558Z",
	"downloads": 1717
}

(76 bytes, without formatting) versus 096a732d62696e617279e000014a6f3b531e86b5 (20 bytes)

On average, a 2x to 3x space reduction can be observed.

In the benchmark you can find another test with more data. In my machine with node 8.9.0, this is the result:

Encode
        JSON
                Time: 2.47ms
                Size: 190KiB
        js-binary
                Time: 1.453ms (-41%)
                Size: 51KiB (3.7x less)
Decode
        JSON
                Time: 2.791ms
        js-binary
                Time: 0.497ms (-82%)

Available types

Basic types

  • 'uint': unsigned integer (between 0 and 2^53)
  • 'int': signed integer (between -2^53 and 2^53)
  • 'float': a 64-bit floating-point (the JavaScript number type)
  • 'string': a utf-8 string
  • 'Buffer': a Buffer instance
  • 'boolean': a boolean
  • 'regex': a JS RegExp instance
  • 'date': a JS Date instance
  • 'json': any data supported by JSON format. Read bellow for more
  • 'oid': mongodb ObjectId (see bellow)

Compound types

A compound type is an object with (optional) fields. Those fields may be arrays, but with the restriction that every element has the same data schema.

Examples:

  • Nested fields: {a: {b: 'int', d: {e: 'int'}}}
  • Optional fields: {a: 'int', 'b?': 'int', 'c?': {d: 'int'}}
  • Array fields: {a: ['int']}
  • All together now: {'a?': [{'b?': 'int'}]}

Array type

An array type in which every element has the same data schema.

Examples:

  • Int array: ['int']
  • Object array: [{v: 'int', f: 'string'}]

JSON type

As stated before, the js-binary requires the data to have a rather strict schema. But sometimes, part of the data may not fit this reality. In this case, you can fallback to JSON :)

Of course, a JSON field will miss the point about space efficiency and data validation, but will gain in flexibility.

ObjectId type

js-binary gives first-class support for mongodb ObjectId. But since js-binary doesn't (and shouldn't) depend on any peculiar mongodb driver, the rules for this type are:

  • Encoding: any object o is accepted, as long new Buffer(String(o), 'hex') yields a 12-byte Buffer
  • Decoding: returns a 24-char hex-encoded string

This should be compatible with most ObjectId implementations on the wild

Spec

The binary format spec is documented in the format.md file