Skip to content

Commit 9c42713

Browse files
dalisoftmcollina
authored andcommitted
[Benchmark]: Add more similar library to benchmark (#171)
* add libraries for benchmark * update benchmark (json-strify target goal changed) * move from optionalDependencies to devDependencies
1 parent 289e050 commit 9c42713

File tree

2 files changed

+85
-12
lines changed

2 files changed

+85
-12
lines changed

bench.js

Lines changed: 82 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,36 @@ const benchmark = require('benchmark')
44
const suite = new benchmark.Suite()
55

66
const schema = {
7-
'title': 'Example Schema',
8-
'type': 'object',
9-
'properties': {
10-
'firstName': {
11-
'type': 'string'
7+
title: 'Example Schema',
8+
type: 'object',
9+
properties: {
10+
firstName: {
11+
type: 'string'
1212
},
13-
'lastName': {
14-
'type': ['string', 'null']
13+
lastName: {
14+
type: ['string', 'null']
1515
},
16-
'age': {
17-
'description': 'Age in years',
18-
'type': 'integer',
19-
'minimum': 0
16+
age: {
17+
description: 'Age in years',
18+
type: 'integer',
19+
minimum: 0
20+
}
21+
}
22+
}
23+
const schemaCJS = {
24+
title: 'Example Schema',
25+
type: 'object',
26+
properties: {
27+
firstName: {
28+
type: 'string'
29+
},
30+
lastName: {
31+
type: ['string', 'null']
32+
},
33+
age: {
34+
description: 'Age in years',
35+
type: 'number',
36+
minimum: 0
2037
}
2138
}
2239
}
@@ -27,6 +44,12 @@ const arraySchema = {
2744
items: schema
2845
}
2946

47+
const arraySchemaCJS = {
48+
title: 'array schema',
49+
type: 'array',
50+
items: schemaCJS
51+
}
52+
3053
const obj = {
3154
firstName: 'Matteo',
3255
lastName: 'Collina',
@@ -35,6 +58,16 @@ const obj = {
3558

3659
const multiArray = []
3760

61+
const JSTR = require('json-strify')
62+
const JSTRStringify = JSTR(schemaCJS)
63+
const JSTRArray = JSTR(arraySchemaCJS)
64+
const JSTRInstance = JSTR()
65+
66+
const CJS = require('compile-json-stringify')
67+
const CJSStringify = CJS(schemaCJS)
68+
const CJSStringifyArray = CJS(arraySchemaCJS)
69+
const CJSStringifyString = CJS({ type: 'string' })
70+
3871
const FJS = require('.')
3972
const stringify = FJS(schema)
4073
const stringifyUgly = FJS(schema, { uglify: true })
@@ -60,6 +93,12 @@ for (i = 0; i < 1000; i++) {
6093
suite.add('FJS creation', function () {
6194
FJS(schema)
6295
})
96+
suite.add('JSTR creation', function () {
97+
JSTR(schemaCJS)
98+
})
99+
suite.add('CJS creation', function () {
100+
CJS(schemaCJS)
101+
})
63102

64103
suite.add('JSON.stringify array', function () {
65104
JSON.stringify(multiArray)
@@ -73,6 +112,14 @@ suite.add('fast-json-stringify-uglified array', function () {
73112
stringifyArrayUgly(multiArray)
74113
})
75114

115+
suite.add('json-strify array', function () {
116+
JSTRArray(multiArray)
117+
})
118+
119+
suite.add('compile-json-stringify array', function () {
120+
CJSStringifyArray(multiArray)
121+
})
122+
76123
suite.add('JSON.stringify long string', function () {
77124
JSON.stringify(str)
78125
})
@@ -85,6 +132,14 @@ suite.add('fast-json-stringify-uglified long string', function () {
85132
stringifyStringUgly(str)
86133
})
87134

135+
suite.add('json-strify long string', function () {
136+
JSTRInstance(str)
137+
})
138+
139+
suite.add('compile-json-stringify long string', function () {
140+
CJSStringifyString(str)
141+
})
142+
88143
suite.add('JSON.stringify short string', function () {
89144
JSON.stringify('hello world')
90145
})
@@ -97,6 +152,14 @@ suite.add('fast-json-stringify-uglified short string', function () {
97152
stringifyStringUgly('hello world')
98153
})
99154

155+
suite.add('json-strify short string', function () {
156+
JSTRInstance('hello world')
157+
})
158+
159+
suite.add('compile-json-stringify short string', function () {
160+
CJSStringifyString('hello world')
161+
})
162+
100163
suite.add('JSON.stringify obj', function () {
101164
JSON.stringify(obj)
102165
})
@@ -109,6 +172,14 @@ suite.add('fast-json-stringify-uglified obj', function () {
109172
stringifyUgly(obj)
110173
})
111174

175+
suite.add('json-strify obj', function () {
176+
JSTRStringify(obj)
177+
})
178+
179+
suite.add('compile-json-stringify obj', function () {
180+
CJSStringify(obj)
181+
})
182+
112183
suite.on('cycle', cycle)
113184

114185
suite.run()

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@
4141
"tap": "^12.6.5",
4242
"tap-mocha-reporter": "^3.0.9",
4343
"typescript": "^3.0.0",
44-
"uglify-es": "^3.3.9"
44+
"uglify-es": "^3.3.9",
45+
"compile-json-stringify": "^0.1.2",
46+
"json-strify": "^0.1.7"
4547
},
4648
"dependencies": {
4749
"ajv": "^6.8.1",

0 commit comments

Comments
 (0)