Skip to content

Instantly share code, notes, and snippets.

@AndreVallestero
Last active August 26, 2021 15:22
Show Gist options
  • Select an option

  • Save AndreVallestero/079447483979b4cfe3cd5d6aa582de98 to your computer and use it in GitHub Desktop.

Select an option

Save AndreVallestero/079447483979b4cfe3cd5d6aa582de98 to your computer and use it in GitHub Desktop.
Javascript [de]serialization benchmark
const fs = require("fs");
const { serialize, deserialize } = require("v8")
// wget https://www.bundler.openkoi.com/state/current
const stateStr = fs.readFileSync("current.json", "utf8");
const state = JSON.parse(stateStr);
const stateStrBuf = Buffer.from(stateStr);
const stateSer = serialize(state);
// Warmup
console.log("Warming up");
for(let i = 0; i < 100; ++i)
JSON.parse(JSON.stringify(stateStr));
console.log("\nSerialization:");
// 9.616s
console.time("str");
for(let i = 0; i < 1000; ++i)
JSON.stringify(state);
console.timeEnd("str");
// 15.611s
console.time("buf");
for(let i = 0; i < 1000; ++i)
Buffer.from(JSON.stringify(state), "utf8");
console.timeEnd("buf");
// 8.844s
console.time("ser");
for(let i = 0; i < 1000; ++i)
serialize(state);
console.timeEnd("ser");
console.log("\nDeserialization:");
// 14.196s
console.time("str");
for(let i = 0; i < 1000; ++i)
JSON.parse(stateStr);
console.timeEnd("str");
// 15.858s
console.time("buf");
for(let i = 0; i < 1000; ++i)
JSON.parse(stateStrBuf);
console.timeEnd("buf");
// 12.379s
console.time("deser")
for(let i = 0; i < 1000; ++i)
deserialize(stateSer);
console.timeEnd("deser");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment