Skip to content

Commit 14f01d3

Browse files
authored
add ajv jtd serialize to benchmark (#362)
1 parent 10100d7 commit 14f01d3

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

bench.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,21 @@ const schemaCJS = {
3838
}
3939
}
4040

41+
const schemaAJVJTD = {
42+
properties: {
43+
firstName: {
44+
type: 'string'
45+
},
46+
lastName: {
47+
type: 'string',
48+
nullable: true
49+
},
50+
age: {
51+
type: 'uint8'
52+
}
53+
}
54+
}
55+
4156
const arraySchema = {
4257
title: 'array schema',
4358
type: 'array',
@@ -50,6 +65,10 @@ const arraySchemaCJS = {
5065
items: schemaCJS
5166
}
5267

68+
const arraySchemaAJVJTD = {
69+
elements: schemaAJVJTD
70+
}
71+
5372
const obj = {
5473
firstName: 'Matteo',
5574
lastName: 'Collina',
@@ -69,6 +88,12 @@ const stringifyArray = FJS(arraySchema)
6988
const stringifyString = FJS({ type: 'string' })
7089
let str = ''
7190

91+
const Ajv = require('ajv/dist/jtd')
92+
const ajv = new Ajv()
93+
const ajvSerialize = ajv.compileSerializer(schemaAJVJTD)
94+
const ajvSerializeArray = ajv.compileSerializer(arraySchemaAJVJTD)
95+
const ajvSerializeString = ajv.compileSerializer({ type: 'string' })
96+
7297
// eslint-disable-next-line
7398
for (var i = 0; i < 10000; i++) {
7499
str += i
@@ -89,6 +114,9 @@ suite.add('FJS creation', function () {
89114
suite.add('CJS creation', function () {
90115
CJS(schemaCJS)
91116
})
117+
suite.add('AJV Serialize creation', function () {
118+
ajv.compileSerializer(schemaAJVJTD)
119+
})
92120

93121
suite.add('JSON.stringify array', function () {
94122
JSON.stringify(multiArray)
@@ -102,6 +130,10 @@ suite.add('compile-json-stringify array', function () {
102130
CJSStringifyArray(multiArray)
103131
})
104132

133+
suite.add('AJV Serialize array', function () {
134+
ajvSerializeArray(multiArray)
135+
})
136+
105137
suite.add('JSON.stringify long string', function () {
106138
JSON.stringify(str)
107139
})
@@ -114,6 +146,10 @@ suite.add('compile-json-stringify long string', function () {
114146
CJSStringifyString(str)
115147
})
116148

149+
suite.add('AJV Serialize long string', function () {
150+
ajvSerializeString(str)
151+
})
152+
117153
suite.add('JSON.stringify short string', function () {
118154
JSON.stringify('hello world')
119155
})
@@ -126,6 +162,10 @@ suite.add('compile-json-stringify short string', function () {
126162
CJSStringifyString('hello world')
127163
})
128164

165+
suite.add('AJV Serialize short string', function () {
166+
ajvSerializeString('hello world')
167+
})
168+
129169
suite.add('JSON.stringify obj', function () {
130170
JSON.stringify(obj)
131171
})
@@ -138,6 +178,10 @@ suite.add('compile-json-stringify obj', function () {
138178
CJSStringify(obj)
139179
})
140180

181+
suite.add('AJV Serialize obj', function () {
182+
ajvSerialize(obj)
183+
})
184+
141185
suite.on('cycle', cycle)
142186

143187
suite.run()

0 commit comments

Comments
 (0)