forked from near/borsh-js
-
Notifications
You must be signed in to change notification settings - Fork 4
/
string-u32-uint8array.ts
58 lines (44 loc) · 1.66 KB
/
string-u32-uint8array.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
import { field, serialize, deserialize } from '../src/index.js'
import B from 'benchmark'
import protobuf from "protobufjs";
import crypto from 'crypto';
// Run with "node --loader ts-node/esm ./benchmark/string-u32-uint8array.ts"
/*
* borsh x 2,587,886 ops/sec ±0.38% (395 runs sampled)
* protobujs x 2,262,857 ops/sec ±0.22% (395 runs sampled)
*/
function getRandomInt(max: number) {
return Math.floor(Math.random() * max);
}
class Test {
@field({ type: 'string' })
name: string;
@field({ type: 'u32' })
age: number;
@field({ type: Uint8Array })
id: Uint8Array;
constructor(name: string, age: number, id: Uint8Array) {
this.name = name;
this.age = age;
this.id = id;
}
}
const protoRoot = protobuf.loadSync('benchmark/string-u32-uint8array.proto')
const ProtoMessage = protoRoot.lookupType("Message");
const createObject = () => {
return new Test("name-🍍" + getRandomInt(254), getRandomInt(254), crypto.randomBytes(32))
}
const numTestObjects = 10000
const testObjects = ((new Array(numTestObjects)).fill(createObject()));
const getTestObject = () => testObjects[getRandomInt(numTestObjects)];
const borshArgs = { unchecked: true, object: true }
const suite = new B.Suite()
suite.add("borsh", () => {
deserialize(serialize(getTestObject()), Test, borshArgs);
}, { minSamples: 300 }).add('protobujs', () => {
ProtoMessage.toObject(ProtoMessage.decode(ProtoMessage.encode(getTestObject()).finish()))
}, { minSamples: 300 }).on('cycle', (event: any) => {
console.log(String(event.target));
}).on('complete', function () {
console.log('Fastest is ' + this.filter('fastest').map('name'));
}).run()