Skip to content

Instantly share code, notes, and snippets.

@eden eden/bench.js
Created Jul 23, 2011

Embed
What would you like to do?
JSON vs msgpack
var msgpack = require('msgpack-0.4');
var DATA_TEMPLATE = {
'abcdef' : Math.random(),
'qqq' : 13,
'19' : [1, 2, 3, 4]
};
var DATA = [];
for (var i = 0; i < 500000; i++) {
DATA.push(JSON.parse(JSON.stringify(DATA_TEMPLATE)));
}
var vs = {
msgpack: {
serialize: msgpack.pack,
deserialize: msgpack.unpack
},
json: {
serialize: JSON.stringify,
deserialize: JSON.parse
},
};
var now, buf, sz, avg;
while (true) {
Object.keys(vs).forEach(function(name) {
var method = vs[name];
method.times = method.times || [];
now = Date.now();
DATA.forEach(function(d) {
buf = method.serialize(d);
});
console.log('%s.serialize %dms (%d bytes)',
name, now = Date.now() - now, buf.length);
method.times.push([now]);
now = Date.now();
DATA.forEach(function(d) {
method.deserialize(buf);
});
console.log('%s.deserialize %dms',
name, now = Date.now() - now);
method.times[method.times.length - 1].push(now);
});
console.log();
Object.keys(vs).forEach(function(name) {
var method = vs[name];
avg = method.times.reduce(function(v, c) {
return v.map(function(w, i) {
return w + c[i];
});
});
console.log('avg %s.serialize %dms', name,
Math.round(avg[0]/method.times.length));
console.log('avg %s.deserialize %dms', name,
Math.round(avg[1]/method.times.length));
});
console.log();
}
msgpack.serialize 4575ms (28 bytes)
msgpack.deserialize 1066ms
json.serialize 1054ms (54 bytes)
json.deserialize 930ms
avg msgpack.serialize 4575ms
avg msgpack.deserialize 1066ms
avg json.serialize 1054ms
avg json.deserialize 930ms
msgpack.serialize 4348ms (28 bytes)
msgpack.deserialize 1450ms
json.serialize 1019ms (54 bytes)
json.deserialize 899ms
avg msgpack.serialize 4462ms
avg msgpack.deserialize 1258ms
avg json.serialize 1037ms
avg json.deserialize 915ms
msgpack.serialize 4146ms (28 bytes)
msgpack.deserialize 1039ms
json.serialize 1089ms (54 bytes)
json.deserialize 937ms
avg msgpack.serialize 4356ms
avg msgpack.deserialize 1185ms
avg json.serialize 1054ms
avg json.deserialize 922ms
msgpack.serialize 4201ms (28 bytes)
msgpack.deserialize 1426ms
json.serialize 1045ms (54 bytes)
json.deserialize 895ms
avg msgpack.serialize 4318ms
avg msgpack.deserialize 1245ms
avg json.serialize 1052ms
avg json.deserialize 915ms
msgpack.serialize 4202ms (28 bytes)
msgpack.deserialize 1026ms
json.serialize 1087ms (54 bytes)
json.deserialize 932ms
avg msgpack.serialize 4294ms
avg msgpack.deserialize 1201ms
avg json.serialize 1059ms
avg json.deserialize 919ms
msgpack.serialize 4160ms (28 bytes)
msgpack.deserialize 1418ms
json.serialize 1031ms (54 bytes)
json.deserialize 895ms
avg msgpack.serialize 4272ms
avg msgpack.deserialize 1238ms
avg json.serialize 1054ms
avg json.deserialize 915ms
msgpack.serialize 4203ms (28 bytes)
msgpack.deserialize 1061ms
json.serialize 1113ms (54 bytes)
json.deserialize 926ms
avg msgpack.serialize 4262ms
avg msgpack.deserialize 1212ms
avg json.serialize 1063ms
avg json.deserialize 916ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.