forked from fastify/fast-json-stringify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.js
281 lines (228 loc) · 5.66 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
'use strict'
const benchmark = require('benchmark')
const suite = new benchmark.Suite()
const STR_LEN = 1e4
const LARGE_ARRAY_SIZE = 2e4
const MULTI_ARRAY_LENGHT = 1e3
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 schemaAJVJTD = {
properties: {
firstName: {
type: 'string'
},
lastName: {
type: 'string',
nullable: true
},
age: {
type: 'uint8'
}
}
}
const arraySchema = {
title: 'array schema',
type: 'array',
items: schema
}
const arraySchemaCJS = {
title: 'array schema',
type: 'array',
items: schemaCJS
}
const arraySchemaAJVJTD = {
elements: schemaAJVJTD
}
const dateFormatSchema = {
description: 'Date of birth',
type: 'string',
format: 'date'
}
const dateFormatSchemaCJS = {
description: 'Date of birth',
type: 'string',
format: 'date'
}
const obj = {
firstName: 'Matteo',
lastName: 'Collina',
age: 32
}
const date = new Date()
const multiArray = new Array(MULTI_ARRAY_LENGHT)
const largeArray = new Array(LARGE_ARRAY_SIZE)
const CJS = require('compile-json-stringify')
const CJSStringify = CJS(schemaCJS)
const CJSStringifyArray = CJS(arraySchemaCJS)
const CJSStringifyDate = CJS(dateFormatSchemaCJS)
const CJSStringifyString = CJS({ type: 'string' })
const FJS = require('.')
const stringify = FJS(schema)
const stringifyArrayDefault = FJS(arraySchema)
const stringifyArrayJSONStringify = FJS(arraySchema, {
largeArrayMechanism: 'json-stringify'
})
const stringifyDate = FJS(dateFormatSchema)
const stringifyString = FJS({ type: 'string' })
let str = ''
const Ajv = require('ajv/dist/jtd')
const ajv = new Ajv()
const ajvSerialize = ajv.compileSerializer(schemaAJVJTD)
const ajvSerializeArray = ajv.compileSerializer(arraySchemaAJVJTD)
const ajvSerializeString = ajv.compileSerializer({ type: 'string' })
const getRandomString = (length) => {
if (!Number.isInteger(length)) {
throw new Error('Expected integer length')
}
const validCharacters = 'abcdefghijklmnopqrstuvwxyz'
const nValidCharacters = 26
let result = ''
for (let i = 0; i < length; ++i) {
result += validCharacters[Math.floor(Math.random() * nValidCharacters)]
}
return result[0].toUpperCase() + result.slice(1)
}
// eslint-disable-next-line
for (let i = 0; i < STR_LEN; i++) {
largeArray[i] = {
firstName: getRandomString(8),
lastName: getRandomString(6),
age: Math.ceil(Math.random() * 99)
}
str += i
if (i % 100 === 0) {
str += '"'
}
}
for (let i = STR_LEN; i < LARGE_ARRAY_SIZE; ++i) {
largeArray[i] = {
firstName: getRandomString(10),
lastName: getRandomString(4),
age: Math.ceil(Math.random() * 99)
}
}
Number(str)
for (let i = 0; i < MULTI_ARRAY_LENGHT; i++) {
multiArray[i] = obj
}
suite.add('FJS creation', function () {
FJS(schema)
})
suite.add('CJS creation', function () {
CJS(schemaCJS)
})
suite.add('AJV Serialize creation', function () {
ajv.compileSerializer(schemaAJVJTD)
})
suite.add('JSON.stringify array', function () {
JSON.stringify(multiArray)
})
suite.add('fast-json-stringify array default', function () {
stringifyArrayDefault(multiArray)
})
suite.add('fast-json-stringify array json-stringify', function () {
stringifyArrayJSONStringify(multiArray)
})
suite.add('compile-json-stringify array', function () {
CJSStringifyArray(multiArray)
})
suite.add('AJV Serialize array', function () {
ajvSerializeArray(multiArray)
})
suite.add('JSON.stringify large array', function () {
JSON.stringify(largeArray)
})
suite.add('fast-json-stringify large array default', function () {
stringifyArrayDefault(largeArray)
})
suite.add('fast-json-stringify large array json-stringify', function () {
stringifyArrayJSONStringify(largeArray)
})
suite.add('compile-json-stringify large array', function () {
CJSStringifyArray(largeArray)
})
suite.add('AJV Serialize large array', function () {
ajvSerializeArray(largeArray)
})
suite.add('JSON.stringify long string', function () {
JSON.stringify(str)
})
suite.add('fast-json-stringify long string', function () {
stringifyString(str)
})
suite.add('compile-json-stringify long string', function () {
CJSStringifyString(str)
})
suite.add('AJV Serialize long string', function () {
ajvSerializeString(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('compile-json-stringify short string', function () {
CJSStringifyString('hello world')
})
suite.add('AJV Serialize short string', function () {
ajvSerializeString('hello world')
})
suite.add('JSON.stringify obj', function () {
JSON.stringify(obj)
})
suite.add('fast-json-stringify obj', function () {
stringify(obj)
})
suite.add('compile-json-stringify obj', function () {
CJSStringify(obj)
})
suite.add('AJV Serialize obj', function () {
ajvSerialize(obj)
})
suite.add('JSON stringify date', function () {
JSON.stringify(date)
})
suite.add('fast-json-stringify date format', function () {
stringifyDate(date)
})
suite.add('compile-json-stringify date format', function () {
CJSStringifyDate(date)
})
suite.on('cycle', cycle)
suite.run()
function cycle (e) {
console.log(e.target.toString())
}