-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialize.bench.ts
97 lines (83 loc) 路 2.34 KB
/
serialize.bench.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { bench } from 'vitest';
import { Parser } from 'binary-parser-encoder';
import Destruct from 'destruct-js';
import Struct from 'structron';
import { array, struct, uint16, uint32 } from '../src/index.js';
// ebin
const EbinPoint = struct({
x: uint16(),
y: uint16(),
z: uint16(),
});
const EbinPoints = struct({
length: uint32(),
points: array(EbinPoint),
})
.littleEndian()
.withCount('points', 'length');
// binary-parser-encoder
const Points = new Parser().uint32le('len').array('points', {
length: 'len',
type: new Parser().uint16le('x').uint16le('y').uint16le('z'),
});
// destruct-js
const spec = new Destruct.Spec({ mode: Destruct.Mode.LE });
spec
.field('len', Destruct.UInt32)
.loop(
'points',
r => r.len,
new Destruct.Spec({ mode: Destruct.Mode.LE })
.field('x', Destruct.UInt16)
.field('y', Destruct.UInt16)
.field('z', Destruct.UInt16),
);
// structron
const PointsStruct = new Struct()
.addMember(Struct.TYPES.UINT_LE, 'len')
.addArray(
new Struct()
.addMember(Struct.TYPES.USHORT_LE, 'x')
.addMember(Struct.TYPES.USHORT_LE, 'y')
.addMember(Struct.TYPES.USHORT_LE, 'z'),
'points',
0,
'len',
);
// Prepare input
const n = 1000;
const points: { x: number; y: number; z: number }[] = [];
for (let i = 0; i < n; i++) {
points.push({ x: 123, y: 456, z: 789 });
}
bench('hand-written - Buffer', () => {
const buf = Buffer.alloc(4 + n * 2 * 3);
buf.writeUInt32LE(points.length, 0);
for (let i = 0; i < points.length; i++) {
buf.writeUInt16LE(points[i].x, i * 6 + 0 + 4);
buf.writeUInt16LE(points[i].y, i * 6 + 2 + 4);
buf.writeUInt16LE(points[i].z, i * 6 + 4 + 4);
}
});
bench('hand-written - DataView', () => {
const buf = new Uint8Array(4 + n * 2 * 3).buffer;
const dataView = new DataView(buf);
dataView.setUint32(points.length, 0, true);
for (let i = 0; i < points.length; i++) {
dataView.setUint16(points[i].x, i * 6 + 0 + 4, true);
dataView.setUint16(points[i].y, i * 6 + 2 + 4, true);
dataView.setUint16(points[i].z, i * 6 + 4 + 4, true);
}
});
bench('ebin', () => {
EbinPoints.toByteArray({ points });
});
bench('binary-parser-encoder', () => {
Points.encode({ points });
});
bench('destruct-js', () => {
spec.write({ len: points.length, points });
});
bench('structron', () => {
PointsStruct.write({ points });
});