-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathbenchmark.js
57 lines (53 loc) · 1.59 KB
/
benchmark.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
const compile = require('./')
const dave = {
checked: true,
checker: false,
dimensions: {
height: 10,
width: 5
},
id: 1,
name: 'A green door',
price: 12
}
const s = JSON.stringify(dave)
const b = Buffer.from(s)
const cnt = 1e7
for (let o = 0; o < 3; o++) {
const veryFast = o > 0
const safe = o < 2
const opts = {ordered: veryFast, required: veryFast, unescapeStrings: !veryFast, fullMatch: safe, validate: safe}
const parseNoBuf = compile(compile.inferRawSchema(dave, opts), opts)
const parse = compile(compile.inferRawSchema(dave, opts), Object.assign({}, opts, {buffer: true}))
if (o) console.log()
console.log('Compiling with', opts)
console.log('One parse', parseNoBuf(s))
console.log('\nBenching from string\n')
for (let r = 0; r < 2; r++) {
console.log('Run ' + r)
console.time('Benching turbo-json-parse from string')
for (let i = 0; i < cnt; i++) {
parseNoBuf(s)
}
console.timeEnd('Benching turbo-json-parse from string')
console.time('Benching JSON.parse from string')
for (let i = 0; i < cnt; i++) {
JSON.parse(s)
}
console.timeEnd('Benching JSON.parse from string')
}
console.log('\nBenching from buffer\n')
for (let r = 0; r < 2; r++) {
console.log('Run ' + r)
console.time('Benching turbo-json-parse from buffer')
for (let i = 0; i < cnt; i++) {
parse(b)
}
console.timeEnd('Benching turbo-json-parse from buffer')
console.time('Benching JSON.parse from buffer')
for (let i = 0; i < cnt; i++) {
JSON.parse(b)
}
console.timeEnd('Benching JSON.parse from buffer')
}
}