Skip to content

Commit abfb49c

Browse files
committed
Add benchmark script
1 parent 3d5dae5 commit abfb49c

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

benchmark/bench.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
var binary = require('binary');
2+
var Benchmark = require('benchmark');
3+
var bp = require('binparse').bp;
4+
var Parser = require('binary-parser').Parser;
5+
var Destruct = require('destruct-js');
6+
const Struct = require('structron');
7+
8+
var suite = new Benchmark.Suite;
9+
10+
// binparse
11+
const PointParser = bp.object('Point', {
12+
x: bp.lu16,
13+
y: bp.lu16,
14+
z: bp.lu16,
15+
});
16+
17+
const PointsParser = bp.object('SimpleObject', {
18+
length: bp.variable('len', bp.lu32),
19+
points: bp.array('Points', PointParser, 'len'),
20+
});
21+
22+
// binary-parser
23+
const Points = new Parser()
24+
.uint32le('len')
25+
.array('points', {
26+
length: 'len',
27+
type: new Parser()
28+
.uint16le('x')
29+
.uint16le('y')
30+
.uint16le('z')
31+
});
32+
33+
// destruct-js
34+
const spec = new Destruct.Spec({mode: Destruct.Mode.LE});
35+
spec.field('len', Destruct.UInt32)
36+
.loop('points', (r) => r.len, new Destruct.Spec({mode: Destruct.Mode.LE})
37+
.field('x', Destruct.UInt16)
38+
.field('y', Destruct.UInt16)
39+
.field('z', Destruct.UInt16));
40+
41+
// structron
42+
const PointsStruct = new Struct()
43+
.addMember(Struct.TYPES.UINT_LE, 'len')
44+
.addArray(
45+
new Struct().addMember(Struct.TYPES.USHORT_LE, 'x')
46+
.addMember(Struct.TYPES.USHORT_LE, 'y')
47+
.addMember(Struct.TYPES.USHORT_LE, 'z'),
48+
'points', 0, 'len'
49+
);
50+
51+
// Prepare input
52+
var n = 1000;
53+
var buf = Buffer.alloc(4 + n * 2 * 3);
54+
55+
buf.writeUInt32LE(n, 0);
56+
for (var i = 0; i < n; i++) {
57+
buf.writeUInt16LE(123, i * 6 + 0 + 4);
58+
buf.writeUInt16LE(456, i * 6 + 2 + 4);
59+
buf.writeUInt16LE(789, i * 6 + 4 + 4);
60+
}
61+
62+
// Run benchmarks
63+
suite
64+
.add('binparse', function() {
65+
const points = PointsParser.read(buf);
66+
})
67+
.add('binary-parser', function() {
68+
const points = Points.parse(buf);
69+
})
70+
.add('destruct-js', function() {
71+
const points = spec.read(buf);
72+
})
73+
.add('structron', function() {
74+
const points = PointsStruct.read(buf);
75+
})
76+
.on('cycle', function(event) {
77+
console.log(String(event.target));
78+
})
79+
.on('complete', function() {
80+
console.log('Fastest is ' + this.filter('fastest').map('name'));
81+
})
82+
.run({ 'async': true });

benchmark/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"dependencies": {
3+
"benchmark": "^2.1.4",
4+
"binary": "0.3.0",
5+
"binary-parser": "1.8.0",
6+
"binparse": "1.1.0",
7+
"destruct-js": "^0.2.9",
8+
"structron": "^0.4.2"
9+
}
10+
}

0 commit comments

Comments
 (0)