forked from fastify/fast-json-stringify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.js
189 lines (153 loc) · 3.64 KB
/
bench.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
'use strict'
const benchmark = require('benchmark')
const suite = new benchmark.Suite()
const schema = {
title: 'Example Schema',
type: 'object',
properties: {
firstName: {
type: 'string'
},
lastName: {
type: ['string', 'null']
},
age: {
description: 'Age in years',
type: 'integer',
minimum: 0
}
}
}
const schemaCJS = {
title: 'Example Schema',
type: 'object',
properties: {
firstName: {
type: 'string'
},
lastName: {
type: ['string', 'null']
},
age: {
description: 'Age in years',
type: 'number',
minimum: 0
}
}
}
const arraySchema = {
title: 'array schema',
type: 'array',
items: schema
}
const arraySchemaCJS = {
title: 'array schema',
type: 'array',
items: schemaCJS
}
const obj = {
firstName: 'Matteo',
lastName: 'Collina',
age: 32
}
const multiArray = []
const JSTR = require('json-strify')
const JSTRStringify = JSTR(schemaCJS)
const JSTRArray = JSTR(arraySchemaCJS)
const JSTRInstance = JSTR()
const CJS = require('compile-json-stringify')
const CJSStringify = CJS(schemaCJS)
const CJSStringifyArray = CJS(arraySchemaCJS)
const CJSStringifyString = CJS({ type: 'string' })
const FJS = require('.')
const stringify = FJS(schema)
const stringifyUgly = FJS(schema, { uglify: true })
const stringifyArray = FJS(arraySchema)
const stringifyArrayUgly = FJS(arraySchema, { uglify: true })
const stringifyString = FJS({ type: 'string' })
const stringifyStringUgly = FJS({ type: 'string', uglify: true })
var str = ''
for (var i = 0; i < 10000; i++) {
str += i
if (i % 100 === 0) {
str += '"'
}
}
Number(str)
for (i = 0; i < 1000; i++) {
multiArray.push(obj)
}
suite.add('FJS creation', function () {
FJS(schema)
})
suite.add('JSTR creation', function () {
JSTR(schemaCJS)
})
suite.add('CJS creation', function () {
CJS(schemaCJS)
})
suite.add('JSON.stringify array', function () {
JSON.stringify(multiArray)
})
suite.add('fast-json-stringify array', function () {
stringifyArray(multiArray)
})
suite.add('fast-json-stringify-uglified array', function () {
stringifyArrayUgly(multiArray)
})
suite.add('json-strify array', function () {
JSTRArray(multiArray)
})
suite.add('compile-json-stringify array', function () {
CJSStringifyArray(multiArray)
})
suite.add('JSON.stringify long string', function () {
JSON.stringify(str)
})
suite.add('fast-json-stringify long string', function () {
stringifyString(str)
})
suite.add('fast-json-stringify-uglified long string', function () {
stringifyStringUgly(str)
})
suite.add('json-strify long string', function () {
JSTRInstance(str)
})
suite.add('compile-json-stringify long string', function () {
CJSStringifyString(str)
})
suite.add('JSON.stringify short string', function () {
JSON.stringify('hello world')
})
suite.add('fast-json-stringify short string', function () {
stringifyString('hello world')
})
suite.add('fast-json-stringify-uglified short string', function () {
stringifyStringUgly('hello world')
})
suite.add('json-strify short string', function () {
JSTRInstance('hello world')
})
suite.add('compile-json-stringify short string', function () {
CJSStringifyString('hello world')
})
suite.add('JSON.stringify obj', function () {
JSON.stringify(obj)
})
suite.add('fast-json-stringify obj', function () {
stringify(obj)
})
suite.add('fast-json-stringify-uglified obj', function () {
stringifyUgly(obj)
})
suite.add('json-strify obj', function () {
JSTRStringify(obj)
})
suite.add('compile-json-stringify obj', function () {
CJSStringify(obj)
})
suite.on('cycle', cycle)
suite.run()
function cycle (e) {
console.log(e.target.toString())
}