Skip to content

Commit 3b207cb

Browse files
mcollinaUzlopak
andauthored
feat: add JSON accelerator to benchmarks (#763)
* add json-accelerator to benchmarks Signed-off-by: Matteo Collina <[email protected]> * fixup Signed-off-by: Matteo Collina <[email protected]> * fixup Signed-off-by: Matteo Collina <[email protected]> * updated readme Signed-off-by: Matteo Collina <[email protected]> * Apply suggestions from code review Signed-off-by: Aras Abbasi <[email protected]> --------- Signed-off-by: Matteo Collina <[email protected]> Signed-off-by: Aras Abbasi <[email protected]> Co-authored-by: Aras Abbasi <[email protected]>
1 parent b53fa6b commit 3b207cb

File tree

3 files changed

+65
-30
lines changed

3 files changed

+65
-30
lines changed

README.md

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
[![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)
66
[![NPM downloads](https://img.shields.io/npm/dm/fast-json-stringify.svg?style=flat)](https://www.npmjs.com/package/fast-json-stringify)
77

8-
98
__fast-json-stringify__ is significantly faster than `JSON.stringify()` for small payloads.
109
Its performance advantage shrinks as your payload grows.
1110
It pairs well with [__flatstr__](https://www.npmjs.com/package/flatstr), which triggers a V8 optimization that improves performance when eventually converting the string to a `Buffer`.
@@ -18,37 +17,43 @@ fast-json-stringify requires a [JSON Schema Draft 7](https://json-schema.org/spe
1817
##### Benchmarks
1918

2019
- Machine: `EX41S-SSD, Intel Core i7, 4Ghz, 64GB RAM, 4C/8T, SSD`.
21-
- Node.js `v18.12.1`
20+
- Node.js `v22.14.0`
2221

2322
```
24-
FJS creation x 4,129 ops/sec ±0.82% (92 runs sampled)
25-
CJS creation x 184,196 ops/sec ±0.12% (97 runs sampled)
26-
AJV Serialize creation x 61,130,591 ops/sec ±0.40% (92 runs sampled)
27-
JSON.stringify array x 5,057 ops/sec ±0.10% (100 runs sampled)
28-
fast-json-stringify array default x 6,243 ops/sec ±0.14% (98 runs sampled)
29-
fast-json-stringify array json-stringify x 6,261 ops/sec ±0.30% (99 runs sampled)
30-
compile-json-stringify array x 6,842 ops/sec ±0.18% (96 runs sampled)
31-
AJV Serialize array x 6,964 ops/sec ±0.11% (95 runs sampled)
32-
JSON.stringify large array x 248 ops/sec ±0.07% (90 runs sampled)
33-
fast-json-stringify large array default x 99.96 ops/sec ±0.22% (74 runs sampled)
34-
fast-json-stringify large array json-stringify x 248 ops/sec ±0.07% (90 runs sampled)
35-
compile-json-stringify large array x 317 ops/sec ±0.09% (89 runs sampled)
36-
AJV Serialize large array x 111 ops/sec ±0.07% (33 runs sampled)
37-
JSON.stringify long string x 16,002 ops/sec ±0.09% (98 runs sampled)
38-
fast-json-stringify long string x 15,979 ops/sec ±0.09% (96 runs sampled)
39-
compile-json-stringify long string x 15,952 ops/sec ±0.31% (97 runs sampled)
40-
AJV Serialize long string x 21,416 ops/sec ±0.08% (98 runs sampled)
41-
JSON.stringify short string x 12,944,272 ops/sec ±0.09% (96 runs sampled)
42-
fast-json-stringify short string x 30,585,790 ops/sec ±0.27% (97 runs sampled)
43-
compile-json-stringify short string x 30,656,406 ops/sec ±0.12% (96 runs sampled)
44-
AJV Serialize short string x 30,406,785 ops/sec ±0.37% (96 runs sampled)
45-
JSON.stringify obj x 3,153,043 ops/sec ±0.33% (99 runs sampled)
46-
fast-json-stringify obj x 6,866,434 ops/sec ±0.11% (100 runs sampled)
47-
compile-json-stringify obj x 15,886,723 ops/sec ±0.15% (98 runs sampled)
48-
AJV Serialize obj x 8,969,043 ops/sec ±0.36% (97 runs sampled)
49-
JSON stringify date x 1,126,547 ops/sec ±0.09% (97 runs sampled)
50-
fast-json-stringify date format x 1,836,188 ops/sec ±0.12% (99 runs sampled)
51-
compile-json-stringify date format x 1,125,735 ops/sec ±0.19% (98 runs sampled)
23+
FJS creation x 9,696 ops/sec ±0.77% (94 runs sampled)
24+
CJS creation x 197,267 ops/sec ±0.22% (95 runs sampled)
25+
AJV Serialize creation x 48,302,927 ops/sec ±2.09% (90 runs sampled)
26+
json-accelerator creation x 668,430 ops/sec ±0.43% (95 runs sampled)
27+
JSON.stringify array x 7,924 ops/sec ±0.11% (98 runs sampled)
28+
fast-json-stringify array default x 7,183 ops/sec ±0.09% (97 runs sampled)
29+
json-accelerator array x 5,762 ops/sec ±0.27% (99 runs sampled)
30+
fast-json-stringify array json-stringify x 7,171 ops/sec ±0.17% (97 runs sampled)
31+
compile-json-stringify array x 6,889 ops/sec ±0.41% (96 runs sampled)
32+
AJV Serialize array x 6,945 ops/sec ±0.17% (98 runs sampled)
33+
JSON.stringify large array x 331 ops/sec ±0.17% (93 runs sampled)
34+
fast-json-stringify large array default x 208 ops/sec ±0.21% (91 runs sampled)
35+
fast-json-stringify large array json-stringify x 330 ops/sec ±0.17% (93 runs sampled)
36+
compile-json-stringify large array x 318 ops/sec ±0.11% (90 runs sampled)
37+
AJV Serialize large array x 114 ops/sec ±0.27% (74 runs sampled)
38+
JSON.stringify long string x 13,452 ops/sec ±0.15% (99 runs sampled)
39+
fast-json-stringify long string x 13,454 ops/sec ±0.10% (99 runs sampled)
40+
json-accelerator long string x 13,439 ops/sec ±0.09% (98 runs sampled)
41+
compile-json-stringify long string x 13,380 ops/sec ±0.12% (100 runs sampled)
42+
AJV Serialize long string x 21,932 ops/sec ±0.06% (99 runs sampled)
43+
JSON.stringify short string x 12,114,052 ops/sec ±0.59% (97 runs sampled)
44+
fast-json-stringify short string x 29,408,175 ops/sec ±1.12% (91 runs sampled)
45+
json-accelerator short string x 29,431,694 ops/sec ±1.05% (93 runs sampled)
46+
compile-json-stringify short string x 24,740,338 ops/sec ±1.02% (91 runs sampled)
47+
AJV Serialize short string x 17,841,869 ops/sec ±0.90% (91 runs sampled)
48+
JSON.stringify obj x 4,577,494 ops/sec ±0.25% (94 runs sampled)
49+
fast-json-stringify obj x 7,291,157 ops/sec ±0.40% (97 runs sampled)
50+
json-accelerator obj x 6,473,194 ops/sec ±0.31% (99 runs sampled)
51+
compile-json-stringify obj x 14,724,935 ops/sec ±0.50% (96 runs sampled)
52+
AJV Serialize obj x 8,782,944 ops/sec ±0.45% (93 runs sampled)
53+
JSON stringify date x 803,522 ops/sec ±0.47% (98 runs sampled)
54+
fast-json-stringify date format x 1,117,776 ops/sec ±0.69% (95 runs sampled)
55+
json-accelerate date format x 1,122,419 ops/sec ±0.20% (97 runs sampled)
56+
compile-json-stringify date format x 803,214 ops/sec ±0.23% (97 runs sampled)
5257
```
5358

5459
#### Table of contents:

benchmark/bench-cmp-lib.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ const ajvSerialize = ajv.compileSerializer(schemaAJVJTD)
118118
const ajvSerializeArray = ajv.compileSerializer(arraySchemaAJVJTD)
119119
const ajvSerializeString = ajv.compileSerializer({ type: 'string' })
120120

121+
const { createAccelerator } = require('json-accelerator')
122+
const accelStringify = createAccelerator(schema)
123+
const accelArray = createAccelerator(arraySchema)
124+
const accelDate = FJS(dateFormatSchema)
125+
const accelString = FJS({ type: 'string' })
126+
121127
const getRandomString = (length) => {
122128
if (!Number.isInteger(length)) {
123129
throw new Error('Expected integer length')
@@ -170,6 +176,9 @@ suite.add('CJS creation', function () {
170176
suite.add('AJV Serialize creation', function () {
171177
ajv.compileSerializer(schemaAJVJTD)
172178
})
179+
suite.add('json-accelerator creation', function () {
180+
createAccelerator(schema)
181+
})
173182

174183
suite.add('JSON.stringify array', function () {
175184
JSON.stringify(multiArray)
@@ -179,6 +188,10 @@ suite.add('fast-json-stringify array default', function () {
179188
stringifyArrayDefault(multiArray)
180189
})
181190

191+
suite.add('json-accelerator array', function () {
192+
accelArray(multiArray)
193+
})
194+
182195
suite.add('fast-json-stringify array json-stringify', function () {
183196
stringifyArrayJSONStringify(multiArray)
184197
})
@@ -219,6 +232,10 @@ suite.add('fast-json-stringify long string', function () {
219232
stringifyString(str)
220233
})
221234

235+
suite.add('json-accelerator long string', function () {
236+
stringifyString(str)
237+
})
238+
222239
suite.add('compile-json-stringify long string', function () {
223240
CJSStringifyString(str)
224241
})
@@ -235,6 +252,10 @@ suite.add('fast-json-stringify short string', function () {
235252
stringifyString('hello world')
236253
})
237254

255+
suite.add('json-accelerator short string', function () {
256+
accelString('hello world')
257+
})
258+
238259
suite.add('compile-json-stringify short string', function () {
239260
CJSStringifyString('hello world')
240261
})
@@ -251,6 +272,10 @@ suite.add('fast-json-stringify obj', function () {
251272
stringify(obj)
252273
})
253274

275+
suite.add('json-accelerator obj', function () {
276+
accelStringify(obj)
277+
})
278+
254279
suite.add('compile-json-stringify obj', function () {
255280
CJSStringify(obj)
256281
})
@@ -267,6 +292,10 @@ suite.add('fast-json-stringify date format', function () {
267292
stringifyDate(date)
268293
})
269294

295+
suite.add('json-accelerate date format', function () {
296+
accelDate(date)
297+
})
298+
270299
suite.add('compile-json-stringify date format', function () {
271300
CJSStringifyDate(date)
272301
})

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"eslint": "^9.17.0",
7676
"fast-json-stringify": ".",
7777
"is-my-json-valid": "^2.20.6",
78+
"json-accelerator": "^0.0.2",
7879
"neostandard": "^0.12.0",
7980
"simple-git": "^3.23.0",
8081
"tsd": "^0.32.0",

0 commit comments

Comments
 (0)