Skip to content

Instantly share code, notes, and snippets.

@maciejhirsz
Last active June 25, 2016 11:30
Show Gist options
  • Save maciejhirsz/89b9813cc3fc875bd0723f6cf85dbbb9 to your computer and use it in GitHub Desktop.
Save maciejhirsz/89b9813cc3fc875bd0723f6cf85dbbb9 to your computer and use it in GitHub Desktop.
Benchmark JSON in Node.js
let json = `{"timestamp":2837513946597,"zone_id":123456,"zone_plan":1,"http":{"protocol":2,"status":200,"host_status":503,"up_status":520,"method":1,"content_type":"text/html","user_agent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36","referer":"https://www.cloudflare.com/","request_uri":"/cdn-cgi/trace"},"origin":{"ip":"1.2.3.4","port":8000,"hostname":"www.example.com","protocol":2},"country":238,"cache_status":3,"server_ip":"192.168.1.1","server_name":"metal.cloudflare.com","remote_ip":"10.1.2.3","bytes_dlv":123456,"ray_id":"10c73629cce30078-LAX"}`;
let data = JSON.parse(json);
const bytes = json.length;
const maxIterations = 500000;
function bench(iter) {
let iterations = maxIterations;
const start = Date.now();
let totalNanos = 0;
while (iterations--) {
data = JSON.parse(json);
json = JSON.stringify(data);
const start = process.hrtime();
iter();
totalNanos += process.hrtime(start)[1];
}
const average = totalNanos / maxIterations;
const iterPerSec = 1e9 / average;
const throughput = (bytes * iterPerSec) / (1024 * 1024);
console.log(`Benching ${iter.name}`);
console.log(`- ${Math.round(average)}ns per iteration`);
console.log(`- throughput ${Math.round(throughput * 100) / 100} MB/s`);
console.log('');
}
bench(function parse() {
out = JSON.parse(json);
});
bench(function serialize() {
out = JSON.stringify(data);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment