Permalink
Browse files

Initial loadSync (node only), see #529

  • Loading branch information...
dcodeIO committed Dec 8, 2016
1 parent 3891ab0 commit 3aa984e063cd73e4687102b4abd8adc16582dbc4
@@ -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);

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
BIN +82 Bytes (100%) dist/protobuf.min.js.gz
Binary file not shown.

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -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:
@@ -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') {
@@ -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
@@ -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");
Oops, something went wrong.

0 comments on commit 3aa984e

Please sign in to comment.