Created
April 7, 2016 06:40
-
-
Save agreatfool/9894fc4159d2a1dd2c893250dc4ae0d3 to your computer and use it in GitHub Desktop.
Compare the performance of two javascript message pack implementation: "msgpack5" & "msgpack-lite"
This file contains 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
"use strict"; | |
const msgpack = require('msgpack5')(); | |
const msgpacklite = require("msgpack-lite"); | |
const obj = {name: "jonathan", age: 29, gender: "male"}; | |
const count = 100000; | |
const try5 = (act) => { | |
if (act === "encode") { | |
for (let i = 0; i < count; i++) { | |
msgpack.encode(obj); | |
} | |
} else { | |
let encoded = msgpack.encode(obj); | |
for (let i = 0; i < count; i++) { | |
msgpack.decode(encoded); | |
} | |
} | |
}; | |
const tryLite = (act) => { | |
if (act === "encode") { | |
for (let i = 0; i < count; i++) { | |
msgpacklite.encode(obj); | |
} | |
} else { | |
let encoded = msgpacklite.encode(obj); | |
for (let i = 0; i < count; i++) { | |
msgpacklite.decode(encoded); | |
} | |
} | |
}; | |
// -------------------------------------------------- | |
const exec = function(func, name, act) { | |
let start = new Date(); | |
func.call(this, act); | |
let consumed = new Date() - start; | |
console.log(name, act, consumed, "ms"); | |
}; | |
exec(try5, "try5", "encode"); | |
exec(try5, "try5", "decode"); | |
exec(tryLite, "tryLite", "encode"); | |
exec(tryLite, "tryLite", "decode"); | |
// try5 encode 1112 ms | |
// try5 decode 1077 ms | |
// tryLite encode 330 ms | |
// tryLite decode 220 ms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment