-
Notifications
You must be signed in to change notification settings - Fork 254
/
main.mjs
106 lines (100 loc) · 2.68 KB
/
main.mjs
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
98
99
100
101
102
103
104
105
106
/* eslint-disable @typescript-eslint/no-var-requires */
import { performance } from 'perf_hooks';
import { runner, systemInfo, getCurrentLocalBSON } from './lib_runner.mjs';
const iterations = 1_000_000;
const startedEntireRun = performance.now();
console.log(systemInfo(iterations));
console.log();
////////////////////////////////////////////////////////////////////////////////////////////////////
await runner({
skip: true,
name: 'deserialize({ oid, string }, { validation: { utf8: false } })',
iterations,
setup(libs) {
const bson = getCurrentLocalBSON(libs);
return Array.from({ length: iterations }, () =>
bson.lib.serialize({
_id: new bson.lib.ObjectId(),
field1: 'value1'
})
);
},
run(i, bson, documents) {
bson.lib.deserialize(documents[i], { validation: { utf8: false } });
}
});
////////////////////////////////////////////////////////////////////////////////////////////////////
await runner({
skip: true,
name: 'new Oid(buf)',
iterations,
setup() {
return Buffer.from('00'.repeat(12), 'hex');
},
run(i, bson, oidBuffer) {
new bson.lib.ObjectId(oidBuffer);
}
});
////////////////////////////////////////////////////////////////////////////////////////////////////
await runner({
skip: true,
name: 'BSON.deserialize(largeDocument)',
iterations,
setup(libs) {
const bson = getCurrentLocalBSON(libs);
const entries = Array.from({ length: 10 }, (_, i) => [
`${i}_${'a'.repeat(10)}`,
'b'.repeat(10)
]);
const document = Object.fromEntries(entries);
const bytes = bson.lib.serialize(document);
console.log(`largeDocument { byteLength: ${bytes.byteLength} }`);
return bytes;
},
run(i, bson, largeDocument) {
new bson.lib.deserialize(largeDocument);
}
});
await runner({
skip: true,
name: 'Double Serialization',
iterations,
run(i, bson) {
bson.lib.serialize({ d: 2.3 });
}
});
await runner({
skip: false,
name: 'Double Deserialization',
iterations,
setup(libs) {
const bson = getCurrentLocalBSON(libs);
return bson.lib.serialize({ d: 2.3 });
},
run(i, bson, serialized_double) {
bson.lib.deserialize(serialized_double);
}
});
await runner({
skip: false,
name: 'Many Doubles Deserialization',
iterations,
setup(libs) {
const bson = getCurrentLocalBSON(libs);
let doubles = Object.fromEntries(
Array.from({ length: 1000 }, i => {
return [`a_${i}`, 2.3];
})
);
return bson.lib.serialize(doubles);
},
run(i, bson, serialized_doubles) {
bson.lib.deserialize(serialized_doubles);
}
});
// End
console.log(
'Total time taken to benchmark:',
(performance.now() - startedEntireRun).toLocaleString(),
'ms'
);