Reputation: 685
I am investigating different data format (string vs masgpack byte) to encode and decode json efficiently. The jsons which I will recieve wont have any schema, atleast to keep it generic, I am assuming the jsons dont follow any specific schema.
Here is a small JS I used to evaluate:
// skip-eslint
var msgpack = require('msgpack5')() // namespace our extensions
, encode = msgpack.encode
, decode = msgpack.decode;
var helloWorldObj = { 'hello': 'world' };
var json = [];
var MAX_DECODE_REPEAT = 10000;
var MAX_REPEAT = 100;
console.log('json,data,time');
//msgpack
for ( var i=0;i<MAX_REPEAT;i++) {
json = [];
// create i+1 length json array
for(var k=0;k<=i;k++){
json.push(helloWorldObj);
}
// encode the json array
var encodedJson = encode(json);
var start = new Date().getTime();
// start decoding =>msgpack
for(var count=0;count<MAX_DECODE_REPEAT;count++){
decode(encodedJson);
}
var end = new Date().getTime();
var time = end-start;
console.log(json.length +',' + encodedJson.length + ','+time);
}
// JSON.parse
for ( var i=0;i<MAX_REPEAT;i++) {
json = [];
// create i+1 length json array
for(var k=0;k<=i;k++){
json.push(helloWorldObj);
}
// stringify the json array
var jsonString = JSON.stringify(json);
var start = new Date().getTime();
// start decoding =>msgpack
for(var count=0;count<MAX_DECODE_REPEAT;count++){
JSON.parse(jsonString);
}
end = new Date().getTime();
time = end-start;
console.log(json.length +',' + jsonString.length*2 + ','+time);
}
The logic is simple: I am increasing the length of the array and trying to find out the time taken by the decoder to decode the array 10000 times.
To my surprise, msgpack is taking 10 times more than json.parse. To decode 100 length array msgpack takes 4694 ms whereas json.parse takes only 378 ms, even though msgpack compresses the array to 1303 bytes from 3602 bytes (assuming each character takes 2 bytes).
What I initially assumed is a smaller size to decode would mean smaller time to decode but its definitely not the case. Any idea why? Do you see any issue with the program to evaluate msgpack?
Thanks
Upvotes: 0
Views: 2266
Reputation: 6418
You're observing this difference because JSON.parse
uses optimized native implementation under the hood, whereas msgpack5
is a pure-JS library. In other languages (i.e., C, Java, etc) MsgPack will be slightly faster in most situations (for example, see this benchmark: https://github.com/eishay/jvm-serializers/wiki).
There are few corner cases when there'll be significant difference. For example, it's likely that msgpack will be faster when serializing numbers/booleans.
Upvotes: 1