Skip to content

Commit

Permalink
Initial loadSync (node only), see #529
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Dec 8, 2016
1 parent 3891ab0 commit 3aa984e
Show file tree
Hide file tree
Showing 11 changed files with 235 additions and 113 deletions.
98 changes: 48 additions & 50 deletions bench/index.js
Expand Up @@ -19,61 +19,59 @@ var protobuf = require("../src/index"),
//
// To experience the impact by yourself, increase string lengths within bench.json.

protobuf.load(require.resolve("./bench.proto"), function onload(err, root) {
var Test = root.lookup("Test");
var root = protobuf.loadSync(require.resolve("./bench.proto"));
var Test = root.lookup("Test");

protobuf.codegen.verbose = true;
protobuf.codegen.verbose = true;

var buf = Test.encode(data).finish();
var buf = Test.encode(data).finish();

// warm up
for (var i = 0; i < 500000; ++i)
Test.encode(data).finish();
for (var i = 0; i < 1000000; ++i)
Test.decode(buf);
console.log("");

// give the optimizer some time to do its job
setTimeout(function() {
var str = JSON.stringify(data),
strbuf = Buffer.from(str, "utf8");
// warm up
for (var i = 0; i < 500000; ++i)
Test.encode(data).finish();
for (var i = 0; i < 1000000; ++i)
Test.decode(buf);
console.log("");

newSuite("encoding")
.add("Type.encode to buffer", function() {
Test.encode(data).finish();
})
.add("JSON.stringify to string", function() {
JSON.stringify(data);
})
.add("JSON.stringify to buffer", function() {
new Buffer(JSON.stringify(data), "utf8");
})
.run();
// give the optimizer some time to do its job
setTimeout(function() {
var str = JSON.stringify(data),
strbuf = Buffer.from(str, "utf8");

newSuite("decoding")
.add("Type.decode from buffer", function() {
Test.decode(buf);
})
.add("JSON.parse from string", function() {
JSON.parse(str);
})
.add("JSON.parse from buffer", function() {
JSON.parse(strbuf.toString("utf8"));
})
.run();
newSuite("encoding")
.add("Type.encode to buffer", function() {
Test.encode(data).finish();
})
.add("JSON.stringify to string", function() {
JSON.stringify(data);
})
.add("JSON.stringify to buffer", function() {
new Buffer(JSON.stringify(data), "utf8");
})
.run();

newSuite("combined")
.add("Type to/from buffer", function() {
Test.decode(Test.encode(data).finish());
})
.add("JSON to/from string", function() {
JSON.parse(JSON.stringify(data));
})
.add("JSON to/from buffer", function() {
JSON.parse(new Buffer(JSON.stringify(data), "utf8").toString("utf8"));
})
.run();
newSuite("decoding")
.add("Type.decode from buffer", function() {
Test.decode(buf);
})
.add("JSON.parse from string", function() {
JSON.parse(str);
})
.add("JSON.parse from buffer", function() {
JSON.parse(strbuf.toString("utf8"));
})
.run();

}, 3000);
newSuite("combined")
.add("Type to/from buffer", function() {
Test.decode(Test.encode(data).finish());
})
.add("JSON to/from string", function() {
JSON.parse(JSON.stringify(data));
})
.add("JSON to/from buffer", function() {
JSON.parse(new Buffer(JSON.stringify(data), "utf8").toString("utf8"));
})
.run();

});
}, 3000);
102 changes: 77 additions & 25 deletions dist/protobuf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/protobuf.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/protobuf.min.js

Large diffs are not rendered by default.

Binary file modified dist/protobuf.min.js.gz
Binary file not shown.
2 changes: 1 addition & 1 deletion dist/protobuf.min.js.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion examples/streaming-rpc.js
Expand Up @@ -85,11 +85,12 @@ greeter.sayHello({ name: 'protocol' });
greeter.sayHello({ name: 'buffers' });
greeter.sayHello(Hello.create({ name: 'for' })); // or use runtime messages

// Listen to and emit your own events if you want:
// Listen to and emit your own events if you like:

greeter.on("status", function(code, text) {
console.log("status:", code, text);
});

greeter.emit("status", 200, "OK");

// And, if applicable, end the service when you are done:
Expand Down
18 changes: 15 additions & 3 deletions src/index.js
Expand Up @@ -7,7 +7,6 @@ var protobuf = global.protobuf = exports;
* @param {Root} root Root namespace, defaults to create a new one if omitted.
* @param {function(?Error, Root=)} callback Callback function
* @returns {undefined}
* @throws {TypeError} If arguments are invalid
*/
function load(filename, root, callback) {
if (typeof root === 'function') {
Expand All @@ -26,7 +25,6 @@ function load(filename, root, callback) {
* @param {string|string[]} filename One or multiple files to load
* @param {function(?Error, Root=)} callback Callback function
* @returns {undefined}
* @throws {TypeError} If arguments are invalid
* @variation 2
*/
// function load(filename:string, callback:function):undefined
Expand All @@ -38,13 +36,27 @@ function load(filename, root, callback) {
* @param {string|string[]} filename One or multiple files to load
* @param {Root} [root] Root namespace, defaults to create a new one if omitted.
* @returns {Promise<Root>} Promise
* @throws {TypeError} If arguments are invalid
* @variation 3
*/
// function load(filename:string, [root:Root]):Promise<Root>

protobuf.load = load;

/**
* Synchronously loads one or multiple .proto or preprocessed .json files into a common root namespace.
* @param {string|string[]} filename One or multiple files to load
* @param {Root} [root] Root namespace, defaults to create a new one if omitted.
* @returns {Root} Root namespace
* @throws {Error} If synchronous fetching is not supported (i.e. in browsers) or if a file's syntax is invalid
*/
function loadSync(filename, root) {
if (!root)
root = new protobuf.Root();
return root.loadSync(filename);
}

protobuf.loadSync = loadSync;

// Parser
protobuf.tokenize = require("./tokenize");
protobuf.parse = require("./parse");
Expand Down

0 comments on commit 3aa984e

Please sign in to comment.