Skip to content

Instantly share code, notes, and snippets.

@atg
Last active August 23, 2018 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atg/cbb093fe91463fb317bdc3cd9bfbfa69 to your computer and use it in GitHub Desktop.
Save atg/cbb093fe91463fb317bdc3cd9bfbfa69 to your computer and use it in GitHub Desktop.
let msgpackLite = require('msgpack-lite');
let borc = require('borc');
let _ = require('lodash');
let { performance } = require('perf_hooks')
let arrays = _.times(100, (n) => {
return _.times(n, i => _.random(10000, 99999, false))
});
let encodedBORC = arrays.map(array => borc.encode(array));
let encodedMSGPACK = arrays.map(array => msgpackLite.encode(array));
let encodedJSON = arrays.map(array => JSON.stringify(array));
function measureEncode(name, codec, print) {
let t0 = performance.now();
for (let i = 0; i < 1000; i++) {
for (let array of arrays) {
codec(array);
}
}
let t1 = performance.now();
if (print) {
console.log(name, t1 - t0);
}
}
function measureDecode(name, codec, encodedArrays, print) {
let t0 = performance.now();
for (let i = 0; i < 10; i++) {
for (let array of encodedArrays) {
codec(array);
}
}
let t1 = performance.now();
if (print) {
console.log(name, t1 - t0);
}
}
console.log("= Encoding Time (ms) =");
// Warm up
measureEncode('msgp.encode ', msgpackLite.encode, false);
measureEncode('borc.encode ', borc.encode, false);
measureEncode('json.stringify', msgpackLite.encode, false);
// Test
measureEncode('msgp.encode ', msgpackLite.encode, true);
measureEncode('borc.encode ', borc.encode, true);
measureEncode('json.stringify', msgpackLite.encode, true);
console.log("\n= Decoding Time (ms) =");
// Warm up
measureDecode('msgp.decode ', msgpackLite.decode, encodedMSGPACK, false);
measureDecode('borc.decodeFirst', borc.decodeFirst, encodedBORC, false);
measureDecode('json.parse ', JSON.parse, encodedJSON, false);
// Test
measureDecode('msgp.decode ', msgpackLite.decode, encodedMSGPACK, true);
measureDecode('borc.decodeFirst', borc.decodeFirst, encodedBORC, true);
measureDecode('json.parse ', JSON.parse, encodedJSON, true);
console.log("\n= Efficiency =");
console.log(_.mean(_.zip(encodedBORC, encodedMSGPACK).map(t => t[0].length - t[1].length)));
console.log('CBOR total bytes', _.sum(encodedBORC.map(x => x.length)));
console.log('MSGPACK total bytes', _.sum(encodedMSGPACK.map(x => x.length)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment