fuwaneko / node-protobuf

Google Protocol Buffers wrapper for Node.js [UNMAINTAINED]
180 stars 42 forks source link

Extremely slow compared to JSON #39

Closed gabrieldodan closed 8 years ago

gabrieldodan commented 10 years ago

Hi, is it normal to run slower than JSON or I am doing something wrong ?

var fs = require("fs");
var p = require("node-protobuf");
var desc = fs.readFileSync("points.desc");
var pb = new p(desc);

var points = [];
for(var i=0; i<100000; i++) {
    points.push({i: 1388534400000, j: 1388534400000});
}

var t1 = Date.now();
var serJSON = JSON.stringify({points: points});
var deserJSON = JSON.parse(serJSON);
var t2 = Date.now();
console.log("Time JSON:", t2-t1);

var t1 = Date.now();
var serPB = pb.serialize({points: points}, 'Points');
var deserPB = pb.parse(serPB, 'Points');
var t2 = Date.now();
console.log("Time PB:", t2-t1);

.proto file

message Point {
    required fixed32 i  = 1;
    required fixed32 j  = 2;
}
message Points {
    repeated Point points = 1;
}

Node version 0.10.29 on Linux. JSON runs two times faster than proto buf !

fuwaneko commented 10 years ago

It's not quite correct to compare JSON and Protocol Buffers. Also, you should run serialization/parsing more than 1 time to measure performance and use high-resolution timers instead of DateTime. And I highly doubt 100000 fields array is a real-life example.

gabrieldodan commented 10 years ago

It is slower even with small arrays, 100 fields array for example. With what should we compare if not with JSON ?

fuwaneko commented 10 years ago

Apache Thrift for example. JSON is native to JS and V8, so it's obviously fast. Also it's much simpler format that protobuf. There really is no point in using protobuf over JSON if your app is Node.js-only. This addon is meant to be used to communicate with existing non-JS apps.

gabrieldodan commented 10 years ago

This module https://github.com/chrisdew/protobuf runs faster. It should be comparable to JSON in my opinion, both are C/C++ code. Also if you make a test on a .proto file like this

message Points {
    repeated fixed32 i = 1;
    repeated fixed32 j = 2;
}

the performance is much much better than JSON!

fuwaneko commented 10 years ago

It should be comparable to JSON in my opinion, both are C/C++ code

Nope, see https://code.google.com/p/v8/source/browse/trunk/src/json.js

I've got some ideas on how to improve performance.