@indutny/protopiler
v2.0.6
Published
Fast zero-dependency protocol buffer parser
Downloads
2,900
Maintainers
Readme
@indutny/protopiler
Just because there are many existing protobuf libraries for JavaScript it doesn't mean that I'm not allowed to create one more.
Selling Points
When compared to protobufjs this library:
- Faster (+5% decode/+32% encode)
- No 3rd party dependencies
- Almost instant compilation
- 2x smaller produced code (after minification)
- Native big integer support
- Native typescript support (via generated .d.ts file)
- Unknown field support
- No classes or prototype inheritance. Just plain JS objects in and out.
Unsupported features
This is unambitious project so we do not intend to support (at least for now):
rpcmap<a, b>- protobuf editions
- extensions/custom options
- ...and probably a lot of other things!
However we do support what we consider to be a typical protobuf usage (scalar, repeated, oneof fields; enums, messages, bytes, strings).
Installation
npm install -g @indutny/protopilerUsage
To compile source protobuf files into javascript run:
protopiler my.proto --output out.mjs --typedefs out.d.mtsIf my.proto has the following contents:
package test;
message MyMessage {
string key = 1;
repeated uint32 values = 2;
}The following JS snippet could be used to encode/decode the MyMessage:
import { test } from './out.mjs';
// Encode JS object into a Uint8Array
const data = test.MyMessage.encode({
key: 'test-key',
values: [1, 2, 3],
});
// Decode Uint8Array back into a JS object
const decoded = test.MyMessage.decode(data);
console.log(decoded);
// Prints:
// { '$unknown': [], key: 'test-key', values: [ 1, 2, 3 ] }Benchmarks
On MacBook Air M2:
$ pnpm install
$ cd benchmarks
$ pnpm start -d 60 -s 1000
protobuf.mjs/decode: 2’386’421.9 ops/sec (±9’533.9, p=0.001, o=68/1000) severe outliers=44
protopiler.mjs/decode: 2’922’101.3 ops/sec (±4’707.1, p=0.001, o=17/1000) severe outliers=1
protobuf.mjs/encode: 1’085’274.8 ops/sec (±1’928.4, p=0.001, o=50/1000) severe outliers=29
protopiler.mjs/encode: 1’380’961.1 ops/sec (±1’598.2, p=0.001, o=13/1000) severe outliers=1LICENSE
This software is licensed under the MIT License.
