Last active
October 8, 2018 10:14
-
-
Save rklaehn/cf9602368f1cd7a374452bd318d32a85 to your computer and use it in GitHub Desktop.
CBOR encoding performance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 => ({ i, a: [String(i * 1234), String(i * 1234), String(i * 1234), String(i * 1234)] })) | |
| }); | |
| 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); | |
| } | |
| } | |
| const decoder = new borc.Decoder() | |
| const borcDecodeFirst = (buffer) => decoder.decodeFirst(buffer) | |
| 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', borcDecodeFirst, encodedBORC, false); | |
| measureDecode('json.parse ', JSON.parse, encodedJSON, false); | |
| // Test | |
| measureDecode('msgp.decode ', msgpackLite.decode, encodedMSGPACK, true); | |
| measureDecode('borc.decodeFirst', borcDecodeFirst, 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))); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ node benchmark2.js | |
| = Encoding Time (ms) = | |
| msgp.encode 9172.339491000399 | |
| borc.encode 20400.477496000007 | |
| json.stringify 9445.365290999413 | |
| = Decoding Time (ms) = | |
| msgp.decode 121.99922000057995 | |
| borc.decodeFirst 383.22095499932766 | |
| json.parse 48.20755399949849 | |
| = Efficiency = | |
| 27.58 | |
| CBOR total bytes 152480 | |
| MSGPACK total bytes 149722 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment