GithubHelp home page GithubHelp logo

protocol-buffers's Introduction

protocol-buffers

Protocol Buffers for Node.js

npm install protocol-buffers

build status dat

Usage

Assuming the following test.proto file exists

enum FOO {
	BAR = 1;
}

message Test {
  required float num  = 1;
  required string payload = 2;
}

message AnotherOne {
	repeated FOO list = 1;
}

Use the above proto file to encode/decode messages by doing

var protobuf = require('protocol-buffers')

// pass a proto file as a buffer/string or pass a parsed protobuf-schema object
var messages = protobuf(fs.readFileSync('test.proto'))

var buf = messages.Test.encode({
	num: 42,
	payload: 'hello world'
});

console.log(buf); // should print a buffer

To decode a message use Test.decode

var obj = messages.Test.decode(buf);
console.log(obj); // should print an object similar to above

Enums are accessed in the same way as messages

var buf = messages.AnotherOne.encode({
	list: [
		messages.FOO.BAR
	]
})

You can also use protocol-buffers/require to require .proto files from disk instead of passing them as buffers

var protobuf = require('protocol-buffers/require')
var messages = protobuf('test.proto') // will load and parse __dirname/test.proto

See the Google Protocol Buffers docs for more information about the available types etc.

Performance

This module is fast.

It uses code generation to build as fast as possible encoders/decoders for the protobuf schema. You can run the benchmarks yourself by doing npm run bench.

On my Macbook Air it gives the following results

Benchmarking JSON (baseline)
  Running object encoding benchmark...
  Encoded 1000000 objects in 2142 ms (466853 enc/s)

  Running object decoding benchmark...
  Decoded 1000000 objects in 970 ms (1030928 dec/s)

  Running object encoding+decoding benchmark...
  Encoded+decoded 1000000 objects in 3131 ms (319387 enc+dec/s)

Benchmarking protocol-buffers
  Running object encoding benchmark...
  Encoded 1000000 objects in 2089 ms (478698 enc/s)

  Running object decoding benchmark...
  Decoded 1000000 objects in 735 ms (1360544 dec/s)

  Running object encoding+decoding benchmark...
  Encoded+decoded 1000000 objects in 2826 ms (353857 enc+dec/s)

Note that JSON parsing/serialization in node is a native function that is really fast.

License

MIT

protocol-buffers's People

Contributors

mafintosh avatar astro avatar tmpvar avatar

Watchers

Navid Nikpour avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.