Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] test: add v8 serialize benchmark #8

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ environment:
- nodejs_version: '6'
- nodejs_version: '8'
- nodejs_version: '10'
- nodejs_version: '11'

install:
- ps: Install-Product node $env:nodejs_version
Expand Down
35 changes: 27 additions & 8 deletions benchmark/decode.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

const Benchmark = require('benchmark');
const benchmarks = require('beautify-benchmark');
const suite = new Benchmark.Suite();

const v8 = require('v8');
const JSONSerialize = require('..');

const json = {
Expand All @@ -18,10 +17,26 @@ const json = {
pid: 81281,
side: 'provider',
timestamp: 1481613276143,
date: new Date(),
buf: Buffer.from('hello buffer 😄'),
// err: new Error('foo error'),
// map: new Map(),
};

const buf1 = new Buffer(JSON.stringify(json));
const buf1 = Buffer.from(JSON.stringify(json));
const buf2 = JSONSerialize.encode(json);
const buf3 = v8.serialize(json);
// console.log(typeof json, buf3)
// for (const k in json) {
// const v = json[k];
// console.log(typeof v, k, v8.serialize(v));
// }

console.log(JSON.parse(buf1));
console.log(JSONSerialize.decode(buf2));
console.log(v8.deserialize(buf3));

const suite = new Benchmark.Suite();

// add tests
suite
Expand All @@ -31,6 +46,9 @@ suite
.add('JSONSerialize.decode(buf2)', function() {
JSONSerialize.decode(buf2);
})
.add('v8.deserialize(buf3)', function() {
v8.deserialize(buf3);
})
.on('cycle', function(event) {
benchmarks.add(event.target);
})
Expand All @@ -42,9 +60,10 @@ suite
})
.run({ async: false });

// node version: v6.9.2, date: Mon Feb 06 2017 21:33:01 GMT+0800 (CST)
// node version: v10.13.0, date: Sun Nov 11 2018 23:05:47 GMT+0800 (China Standard Time)
// Starting...
// 2 tests completed.

// JSON.parse(buf1.toString()) x 464,798 ops/sec ±1.62% (90 runs sampled)
// JSONSerialize.decode(buf2) x 45,589 ops/sec ±1.21% (90 runs sampled)
// 3 tests completed.
//
// JSON.parse(buf1.toString()) x 314,187 ops/sec ±0.96% (92 runs sampled)
// JSONSerialize.decode(buf2) x 56,123 ops/sec ±1.31% (88 runs sampled)
// v8.deserialize(buf3) x 176,858 ops/sec ±9.81% (68 runs sampled)
18 changes: 13 additions & 5 deletions benchmark/encode.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const benchmarks = require('beautify-benchmark');
const suite = new Benchmark.Suite();

const JSONSerialize = require('..');
const v8 = require('v8');

const json = {
anyhost: true,
Expand All @@ -18,10 +19,13 @@ const json = {
pid: 81281,
side: 'provider',
timestamp: 1481613276143,
date: new Date(),
buf: Buffer.from('hello buffer 😄'),
};

console.log(new Buffer(JSON.stringify(json)).length);
console.log(JSONSerialize.encode(json).length);
console.log(v8.serialize(json).length);

// add tests
suite
Expand All @@ -31,6 +35,9 @@ suite
.add('JSONSerialize.encode(json)', function() {
JSONSerialize.encode(json);
})
.add('v8.serialize(json)', function() {
v8.serialize(json);
})
.on('cycle', function(event) {
benchmarks.add(event.target);
})
Expand All @@ -42,9 +49,10 @@ suite
})
.run({ async: false });

// node version: v6.9.2, date: Mon Feb 06 2017 21:31:12 GMT+0800 (CST)
// node version: v10.13.0, date: Sun Nov 11 2018 23:10:25 GMT+0800 (China Standard Time)
// Starting...
// 2 tests completed.

// new Buffer(JSON.stringify(json)) x 365,334 ops/sec ±1.44% (87 runs sampled)
// JSONSerialize.encode(json) x 43,591 ops/sec ±1.23% (85 runs sampled)
// 3 tests completed.
//
// new Buffer(JSON.stringify(json)) x 186,784 ops/sec ±1.18% (86 runs sampled)
// JSONSerialize.encode(json) x 53,639 ops/sec ±1.16% (88 runs sampled)
// v8.serialize(json) x 3,921 ops/sec ±46.68% (11 runs sampled)