Skip to content

Commit ce946e7

Browse files
committed
Improve performance of stringifying objects (again)
1 parent 783316d commit ce946e7

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

README.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![Build Status](https://travis-ci.org/nwoltman/compile-json-stringify.svg?branch=master)](https://travis-ci.org/nwoltman/compile-json-stringify)
55
[![Coverage Status](https://coveralls.io/repos/nwoltman/compile-json-stringify/badge.svg?branch=master)](https://coveralls.io/r/nwoltman/compile-json-stringify?branch=master)
66

7-
Inspired by [`fast-json-stringify`](https://github.com/fastify/fast-json-stringify), this module allows you to compile a function that will stringify a JSON payload **2x-4x faster** than `JSON.stringify()` (up to 8.5x faster in one case). To get such high performance, you compile the function with a schema that describes the shape of the data that you want to stringify.
7+
Inspired by [`fast-json-stringify`](https://github.com/fastify/fast-json-stringify), this module allows you to compile a function that will stringify a JSON payload **2x-5x faster** than `JSON.stringify()` (up to 8.5x faster in one case). To get such high performance, you compile the function with a schema that describes the shape of the data that you want to stringify.
88

99
> The difference between `compile-json-stringify` and `fast-json-stringify` is that with `fast-json-stringify` you define the shape of the _output_ data, whereas with this module you define the shape of the _input_ data.
1010
@@ -315,18 +315,19 @@ stringify({name: 'table', length: 32}); // -> '{"name":"table","length":32}'
315315
// Matched 'object'
316316
```
317317

318+
318319
## Benchmark Results
319320

320321
Run on Node 9.4.0
321322

322323
```
323-
1) object - JSON.stringify x 1,966,447 ops/sec ±0.58% (89 runs sampled)
324-
1) object - compile-json-stringify x 7,555,458 ops/sec ±0.43% (91 runs sampled)
325-
1) object - compile-json-stringify coerceTypes x 7,690,803 ops/sec ±0.60% (94 runs sampled)
324+
1) object - JSON.stringify x 1,951,961 ops/sec ±0.63% (93 runs sampled)
325+
1) object - compile-json-stringify x 7,918,447 ops/sec ±0.53% (97 runs sampled)
326+
1) object - compile-json-stringify coerceTypes x 8,283,659 ops/sec ±0.51% (94 runs sampled)
326327
327-
2) array of objects - JSON.stringify x 32,720 ops/sec ±0.41% (95 runs sampled)
328-
2) array of objects - compile-json-stringify x 88,136 ops/sec ±0.79% (96 runs sampled)
329-
2) array of objects - compile-json-stringify coerceTypes x 90,879 ops/sec ±0.56% (97 runs sampled)
328+
2) array of objects - JSON.stringify x 32,524 ops/sec ±0.76% (96 runs sampled)
329+
2) array of objects - compile-json-stringify x 95,166 ops/sec ±0.61% (95 runs sampled)
330+
2) array of objects - compile-json-stringify coerceTypes x 95,651 ops/sec ±1.88% (89 runs sampled)
330331
331332
3) array of numbers - JSON.stringify x 2,458,982 ops/sec ±0.52% (96 runs sampled)
332333
3) array of numbers - compile-json-stringify x 5,539,276 ops/sec ±0.42% (96 runs sampled)
@@ -348,7 +349,7 @@ Run on Node 9.4.0
348349
7) multiple types - compile-json-stringify x 26,271,332 ops/sec ±0.48% (94 runs sampled)
349350
7) multiple types - compile-json-stringify coerceTypes x 26,056,262 ops/sec ±0.58% (94 runs sampled)
350351
351-
8) multiple types in an object - JSON.stringify x 1,031,019 ops/sec ±0.59% (92 runs sampled)
352-
8) multiple types in an object - compile-json-stringify x 3,850,184 ops/sec ±0.44% (91 runs sampled)
353-
8) multiple types in an object - compile-json-stringify coerceTypes x 4,060,270 ops/sec ±0.59% (93 runs sampled)
352+
8) multiple types in an object - JSON.stringify x 992,784 ops/sec ±0.55% (98 runs sampled)
353+
8) multiple types in an object - compile-json-stringify x 4,765,136 ops/sec ±0.48% (96 runs sampled)
354+
8) multiple types in an object - compile-json-stringify coerceTypes x 4,834,149 ops/sec ±0.48% (96 runs sampled)
354355
```

compile-json-stringify.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -353,25 +353,32 @@ class CodeBuilder {
353353

354354
for (const key in properties) {
355355
const accessor = buildAccessor(key);
356+
const stringifiedKey = stringifyPropertyKey(key);
356357
const schemaFn = this.buildSchemaFn(properties[key]);
357358

358359
code = schemaFn.code + code;
359360

360361
if (coerceTypes) {
361362
code += `
362363
if (obj${accessor} !== undefined) {
363-
if (addComma) str += ','
364-
else addComma = true
365-
str += '${stringifyPropertyKey(key)}:' + ${schemaFn.name}(obj${accessor})
364+
if (addComma) {
365+
str += ',${stringifiedKey}:' + ${schemaFn.name}(obj${accessor})
366+
} else {
367+
addComma = true
368+
str += '${stringifiedKey}:' + ${schemaFn.name}(obj${accessor})
369+
}
366370
}`;
367371
} else {
368372
code += `
369373
if (obj${accessor} !== undefined) {
370374
val = ${schemaFn.name}(obj${accessor})
371375
if (val !== undefined) {
372-
if (addComma) str += ','
373-
else addComma = true
374-
str += '${stringifyPropertyKey(key)}:' + val
376+
if (addComma) {
377+
str += ',${stringifiedKey}:' + val
378+
} else {
379+
addComma = true
380+
str += '${stringifiedKey}:' + val
381+
}
375382
}
376383
}`;
377384
}

0 commit comments

Comments
 (0)