-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
index.js
62 lines (48 loc) · 1.47 KB
/
index.js
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
const protobuf = require('../')
const fs = require('fs')
const path = require('path')
const messages = protobuf(fs.readFileSync(path.join(__dirname, 'bench.proto')))
const TIMES = 1000000
let then = 0
let diff = 0
const run = function (name, encode, decode) {
const EXAMPLE = {
foo: 'hello',
hello: 42,
payload: Buffer.from('a'),
meh: {
b: {
tmp: {
baz: 1000
}
},
lol: 'lol'
}
}
const EXAMPLE_BUFFER = encode(EXAMPLE)
let i
console.log('Benchmarking %s', name)
console.log(' Running object encoding benchmark...')
then = Date.now()
for (i = 0; i < TIMES; i++) {
encode(EXAMPLE)
}
diff = Date.now() - then
console.log(' Encoded %d objects in %d ms (%d enc/s)\n', TIMES, diff, (1000 * TIMES / diff).toFixed(0))
console.log(' Running object decoding benchmark...')
then = Date.now()
for (i = 0; i < TIMES; i++) {
decode(EXAMPLE_BUFFER)
}
diff = Date.now() - then
console.log(' Decoded %d objects in %d ms (%d dec/s)\n', TIMES, diff, (1000 * TIMES / diff).toFixed(0))
console.log(' Running object encoding+decoding benchmark...')
then = Date.now()
for (i = 0; i < TIMES; i++) {
decode(encode(EXAMPLE))
}
diff = Date.now() - then
console.log(' Encoded+decoded %d objects in %d ms (%d enc+dec/s)\n', TIMES, diff, (1000 * TIMES / diff).toFixed(0))
}
run('JSON (baseline)', JSON.stringify, JSON.parse)
run('protocol-buffers', messages.Test.encode, messages.Test.decode)