diff --git a/benchmark-flattened.md b/benchmark-flattened.md new file mode 100644 index 0000000..ceb421a --- /dev/null +++ b/benchmark-flattened.md @@ -0,0 +1,120 @@ + +> slow-json-stringify@2.0.1 benchmark /Users/luca.gesmundo/me/slow-json-stringify +> cd benchmark && sh run.sh + + +# Benchmarks + +Benchmarks performed on: +- native **JSON.stringify** +- **fast-json-stringify** +- **slow-json-stringify** + + +## small-object + +```bash +native x 3,256,671 ops/sec ±0.64% (91 runs sampled) +fast-json-stringify x 46,090,717 ops/sec ±0.22% (95 runs sampled) +slow-json-stringify x 46,056,000 ops/sec ±0.65% (92 runs sampled) + +# fast-json-stringify,slow-json-stringify is +0.08% faster + +``` + +## small-array + +```bash +native x 272,422 ops/sec ±0.24% (96 runs sampled) +fast-json-stringify x 345,767 ops/sec ±0.44% (97 runs sampled) +slow-json-stringify x 959,721 ops/sec ±0.53% (89 runs sampled) + +# slow-json-stringify is +177.56% faster + +``` + +## nested-props-short-text + +```bash +native x 693,847 ops/sec ±0.43% (93 runs sampled) +fast-json-stringify x 3,258,606 ops/sec ±0.25% (94 runs sampled) +slow-json-stringify x 10,205,115 ops/sec ±0.96% (91 runs sampled) + +# slow-json-stringify is +213.17% faster + +``` + +## much-props-short-text + +```bash +native x 575,735 ops/sec ±1.03% (91 runs sampled) +fast-json-stringify x 724,984 ops/sec ±1.31% (89 runs sampled) +slow-json-stringify x 2,137,405 ops/sec ±0.75% (89 runs sampled) + +# slow-json-stringify is +194.82% faster + +``` + +## much-props-big-text + +```bash +native x 13,479 ops/sec ±4.24% (83 runs sampled) +fast-json-stringify x 13,100 ops/sec ±3.16% (83 runs sampled) +slow-json-stringify x 2,260,389 ops/sec ±1.37% (92 runs sampled) + +# slow-json-stringify is +16670.95% faster + +``` + +## big-text + +```bash +native x 192,588 ops/sec ±0.50% (89 runs sampled) +fast-json-stringify x 200,919 ops/sec ±0.69% (90 runs sampled) +slow-json-stringify x 44,768,200 ops/sec ±1.21% (91 runs sampled) + +# slow-json-stringify is +22181.83% faster + +``` + +## big-array-short-text + +```bash +native x 3,717 ops/sec ±0.49% (89 runs sampled) +slow-json-stringify x 8,777 ops/sec ±1.55% (89 runs sampled) + +# slow-json-stringify is +136.13% faster + +``` + +## big-array-long-text + +```bash +native x 76.69 ops/sec ±0.72% (74 runs sampled) +slow-json-stringify x 8,595 ops/sec ±0.58% (89 runs sampled) + +# slow-json-stringify is +11209.21% faster + +``` + +## big-array-long-number + +```bash +native x 871 ops/sec ±2.96% (85 runs sampled) +slow-json-stringify x 2,643 ops/sec ±1.39% (88 runs sampled) + +# slow-json-stringify is +203.68% faster + +``` + +## undefined properties + +```bash +native x 1,541,588 ops/sec ±6.02% (77 runs sampled) +fast-json-stringify x 8,287,034 ops/sec ±1.53% (91 runs sampled) +slow-json-stringify x 14,304,614 ops/sec ±1.23% (84 runs sampled) + +# slow-json-stringify is +72.61% faster + +``` + diff --git a/benchmark-normal.md b/benchmark-normal.md new file mode 100644 index 0000000..89c0e69 --- /dev/null +++ b/benchmark-normal.md @@ -0,0 +1,120 @@ + +> slow-json-stringify@2.0.1 benchmark /Users/luca.gesmundo/me/slow-json-stringify +> cd benchmark && sh run.sh + + +# Benchmarks + +Benchmarks performed on: +- native **JSON.stringify** +- **fast-json-stringify** +- **slow-json-stringify** + + +## small-object + +```bash +native x 3,730,490 ops/sec ±2.84% (91 runs sampled) +fast-json-stringify x 55,217,937 ops/sec ±0.12% (95 runs sampled) +slow-json-stringify x 56,829,533 ops/sec ±0.59% (94 runs sampled) + +# slow-json-stringify is +2.92% faster + +``` + +## small-array + +```bash +native x 339,112 ops/sec ±0.53% (88 runs sampled) +fast-json-stringify x 425,930 ops/sec ±0.53% (97 runs sampled) +slow-json-stringify x 1,192,221 ops/sec ±1.09% (92 runs sampled) + +# slow-json-stringify is +179.91% faster + +``` + +## nested-props-short-text + +```bash +native x 838,737 ops/sec ±0.66% (91 runs sampled) +fast-json-stringify x 3,577,531 ops/sec ±0.55% (93 runs sampled) +slow-json-stringify x 11,501,223 ops/sec ±0.36% (92 runs sampled) + +# slow-json-stringify is +221.48% faster + +``` + +## much-props-short-text + +```bash +native x 745,820 ops/sec ±0.35% (93 runs sampled) +fast-json-stringify x 860,018 ops/sec ±1.13% (86 runs sampled) +slow-json-stringify x 2,348,637 ops/sec ±0.65% (84 runs sampled) + +# slow-json-stringify is +173.09% faster + +``` + +## much-props-big-text + +```bash +native x 19,760 ops/sec ±0.80% (84 runs sampled) +fast-json-stringify x 18,744 ops/sec ±0.78% (93 runs sampled) +slow-json-stringify x 3,195,613 ops/sec ±0.44% (95 runs sampled) + +# slow-json-stringify is +16072.13% faster + +``` + +## big-text + +```bash +native x 252,588 ops/sec ±0.52% (92 runs sampled) +fast-json-stringify x 263,067 ops/sec ±0.53% (94 runs sampled) +slow-json-stringify x 57,227,748 ops/sec ±0.65% (90 runs sampled) + +# slow-json-stringify is +21654.14% faster + +``` + +## big-array-short-text + +```bash +native x 4,381 ops/sec ±0.40% (96 runs sampled) +slow-json-stringify x 10,782 ops/sec ±0.37% (96 runs sampled) + +# slow-json-stringify is +146.09% faster + +``` + +## big-array-long-text + +```bash +native x 95.06 ops/sec ±0.73% (80 runs sampled) +slow-json-stringify x 10,981 ops/sec ±0.37% (96 runs sampled) + +# slow-json-stringify is +11458.95% faster + +``` + +## big-array-long-number + +```bash +native x 1,145 ops/sec ±0.35% (94 runs sampled) +slow-json-stringify x 3,746 ops/sec ±0.95% (93 runs sampled) + +# slow-json-stringify is +227.16% faster + +``` + +## undefined properties + +```bash +native x 2,366,286 ops/sec ±0.16% (95 runs sampled) +fast-json-stringify x 7,097,273 ops/sec ±0.19% (94 runs sampled) +slow-json-stringify x 18,652,897 ops/sec ±0.38% (94 runs sampled) + +# slow-json-stringify is +162.82% faster + +``` + diff --git a/benchmark-relative-flattened.md b/benchmark-relative-flattened.md new file mode 100644 index 0000000..6a5eb3c --- /dev/null +++ b/benchmark-relative-flattened.md @@ -0,0 +1,123 @@ + +> slow-json-stringify@2.0.1 benchmark /Users/luca.gesmundo/me/slow-json-stringify +> cd benchmark && sh run.sh + + +# Benchmarks + +Benchmarks performed on: +- native **JSON.stringify** +- **fast-json-stringify** +- **slow-json-stringify** + + +## small-object + +```bash +native x 3,633,001 ops/sec ±2.96% (89 runs sampled) +slow-json-stringify x 52,006,413 ops/sec ±1.01% (95 runs sampled) +slow-json-stringify-flatten x 51,590,192 ops/sec ±0.77% (95 runs sampled) + +# slow-json-stringify is +0.81% faster + +``` + +## small-array + +```bash +native x 316,794 ops/sec ±5.31% (84 runs sampled) +slow-json-stringify x 1,014,889 ops/sec ±1.34% (85 runs sampled) +slow-json-stringify-flatten x 1,105,377 ops/sec ±0.58% (89 runs sampled) + +# slow-json-stringify-flatten is +8.92% faster + +``` + +## nested-props-short-text + +```bash +native x 824,463 ops/sec ±1.19% (89 runs sampled) +slow-json-stringify x 10,993,159 ops/sec ±0.29% (90 runs sampled) +slow-json-stringify-flatten x 11,585,080 ops/sec ±0.96% (93 runs sampled) + +# slow-json-stringify-flatten is +5.38% faster + +``` + +## much-props-short-text + +```bash +native x 751,831 ops/sec ±0.80% (95 runs sampled) +slow-json-stringify x 2,455,491 ops/sec ±0.91% (89 runs sampled) +slow-json-stringify-flatten x 2,515,419 ops/sec ±0.46% (93 runs sampled) + +# slow-json-stringify-flatten is +2.44% faster + +``` + +## much-props-big-text + +```bash +native x 20,618 ops/sec ±0.71% (92 runs sampled) +slow-json-stringify x 2,756,923 ops/sec ±0.51% (94 runs sampled) +slow-json-stringify-flatten x 2,824,019 ops/sec ±0.56% (90 runs sampled) + +# slow-json-stringify-flatten is +2.43% faster + +``` + +## big-text + +```bash +native x 255,721 ops/sec ±0.47% (89 runs sampled) +slow-json-stringify x 56,802,176 ops/sec ±0.17% (97 runs sampled) +slow-json-stringify-flatten x 56,594,977 ops/sec ±0.43% (94 runs sampled) + +# slow-json-stringify,slow-json-stringify-flatten is +0.37% faster + +``` + +## big-array-short-text + +```bash +native x 4,393 ops/sec ±0.81% (95 runs sampled) +slow-json-stringify x 9,518 ops/sec ±1.33% (90 runs sampled) +slow-json-stringify-flatten x 9,492 ops/sec ±0.44% (91 runs sampled) + +# slow-json-stringify-flatten,slow-json-stringify is +0.27% faster + +``` + +## big-array-long-text + +```bash +native x 92.41 ops/sec ±0.97% (78 runs sampled) +slow-json-stringify x 9,519 ops/sec ±0.73% (89 runs sampled) +slow-json-stringify-flatten x 9,560 ops/sec ±0.44% (93 runs sampled) + +# slow-json-stringify-flatten,slow-json-stringify is +0.43% faster + +``` + +## big-array-long-number + +```bash +native x 1,102 ops/sec ±0.67% (90 runs sampled) +slow-json-stringify x 3,089 ops/sec ±1.81% (88 runs sampled) +slow-json-stringify-flatten x 3,300 ops/sec ±1.27% (87 runs sampled) + +# slow-json-stringify-flatten is +6.87% faster + +``` + +## undefined properties + +```bash +native x 2,372,475 ops/sec ±0.36% (90 runs sampled) +slow-json-stringify x 19,522,029 ops/sec ±1.03% (94 runs sampled) +slow-json-stringify-flatten x 20,519,716 ops/sec ±0.36% (94 runs sampled) + +# slow-json-stringify-flatten is +5.11% faster + +``` + diff --git a/benchmark/big-array-long-number.js b/benchmark/big-array-long-number.js index 12a1df6..9b678dc 100644 --- a/benchmark/big-array-long-number.js +++ b/benchmark/big-array-long-number.js @@ -41,6 +41,7 @@ console.log('```bash'); suite .add('native', () => JSON.stringify(obj)) + .add('slow-json-stringify-flatten', () => slowStringify(obj, true)) .add('slow-json-stringify', () => slowStringify(obj)) .on('cycle', (event) => { res.push(Math.floor(event.target.hz)); diff --git a/benchmark/big-array-long-text.js b/benchmark/big-array-long-text.js index a780d4c..c973f12 100644 --- a/benchmark/big-array-long-text.js +++ b/benchmark/big-array-long-text.js @@ -42,6 +42,7 @@ console.log('```bash'); // console.log(slowStringify(obj)); suite .add('native', () => JSON.stringify(obj)) + .add('slow-json-stringify-flatten', () => slowStringify(obj, true)) .add('slow-json-stringify', () => slowStringify(obj)) .on('cycle', (event) => { res.push(Math.floor(event.target.hz)); diff --git a/benchmark/big-array-short-text.js b/benchmark/big-array-short-text.js index f251733..6f70530 100644 --- a/benchmark/big-array-short-text.js +++ b/benchmark/big-array-short-text.js @@ -43,6 +43,7 @@ console.log('```bash'); // console.log(slowStringify(obj)); suite .add('native', () => JSON.stringify(obj)) + .add('slow-json-stringify-flatten', () => slowStringify(obj, true)) .add('slow-json-stringify', () => slowStringify(obj)) .on('cycle', (event) => { res.push(Math.floor(event.target.hz)); diff --git a/benchmark/big-text.js b/benchmark/big-text.js index 6988d70..9eeb746 100644 --- a/benchmark/big-text.js +++ b/benchmark/big-text.js @@ -40,7 +40,7 @@ console.log('```bash'); suite .add('native', () => JSON.stringify(obj)) - .add('fast-json-stringify', () => fastStringify(obj)) + .add('slow-json-stringify-flatten', () => slowStringify(obj, true)) .add('slow-json-stringify', () => slowStringify(obj)) .on('cycle', (event) => { res.push(Math.floor(event.target.hz)); diff --git a/benchmark/much-props-big-text.js b/benchmark/much-props-big-text.js index f1a2867..28716a4 100644 --- a/benchmark/much-props-big-text.js +++ b/benchmark/much-props-big-text.js @@ -80,7 +80,7 @@ console.log('```bash'); suite .add('native', () => JSON.stringify(obj)) - .add('fast-json-stringify', () => fastStringify(obj)) + .add('slow-json-stringify-flatten', () => slowStringify(obj, true)) .add('slow-json-stringify', () => slowStringify(obj)) .on('cycle', (event) => { res.push(Math.floor(event.target.hz)); diff --git a/benchmark/much-props-short-text.js b/benchmark/much-props-short-text.js index e39c272..9aaf700 100644 --- a/benchmark/much-props-short-text.js +++ b/benchmark/much-props-short-text.js @@ -81,7 +81,7 @@ console.log('```bash'); suite .add('native', () => JSON.stringify(obj)) - .add('fast-json-stringify', () => fastStringify(obj)) + .add('slow-json-stringify-flatten', () => slowStringify(obj, true)) .add('slow-json-stringify', () => slowStringify(obj)) .on('cycle', (event) => { res.push(Math.floor(event.target.hz)); diff --git a/benchmark/nested-props-short-text.js b/benchmark/nested-props-short-text.js index 7a8050d..818d21e 100644 --- a/benchmark/nested-props-short-text.js +++ b/benchmark/nested-props-short-text.js @@ -86,7 +86,7 @@ console.log('```bash'); suite .add('native', () => JSON.stringify(obj)) - .add('fast-json-stringify', () => fastStringify(obj)) + .add('slow-json-stringify-flatten', () => slowStringify(obj, true)) .add('slow-json-stringify', () => slowStringify(obj)) .on('cycle', (event) => { res.push(Math.floor(event.target.hz)); diff --git a/benchmark/small-array.js b/benchmark/small-array.js index ff0c403..3989731 100644 --- a/benchmark/small-array.js +++ b/benchmark/small-array.js @@ -100,7 +100,7 @@ console.log('```bash'); suite .add('native', () => JSON.stringify(obj)) - .add('fast-json-stringify', () => fastStringify(obj)) + .add('slow-json-stringify-flatten', () => slowStringify(obj, true)) .add('slow-json-stringify', () => slowStringify(obj)) .on('cycle', (event) => { res.push(Math.floor(event.target.hz)); diff --git a/benchmark/small-object.js b/benchmark/small-object.js index 533192a..6df04e4 100644 --- a/benchmark/small-object.js +++ b/benchmark/small-object.js @@ -37,7 +37,7 @@ console.log('```bash'); suite .add('native', () => JSON.stringify(obj)) - .add('fast-json-stringify', () => fastStringify(obj)) + .add('slow-json-stringify-flatten', () => slowStringify(obj, true)) .add('slow-json-stringify', () => slowStringify(obj)) .on('cycle', (event) => { res.push(Math.floor(event.target.hz)); diff --git a/benchmark/undef.js b/benchmark/undef.js index 9db96ab..59db860 100644 --- a/benchmark/undef.js +++ b/benchmark/undef.js @@ -46,7 +46,7 @@ console.log('```bash'); suite .add('native', () => JSON.stringify(obj)) - .add('fast-json-stringify', () => fastStringify(obj)) + .add('slow-json-stringify-flatten', () => slowStringify(obj, true)) .add('slow-json-stringify', () => slowStringify(obj)) .on('cycle', (event) => { res.push(Math.floor(event.target.hz)); diff --git a/src/sjs.mjs b/src/sjs.mjs index 3781a23..abc9f78 100644 --- a/src/sjs.mjs +++ b/src/sjs.mjs @@ -6,14 +6,31 @@ import { attr, escape } from './_utils'; // Doing a lot of preparation work before returning the final function responsible for // the stringification. -const sjs = (schema) => { +const sjs = (schema, flatten = false) => { const { preparedString, preparedSchema } = _prepare(schema); // Providing preparedSchema for univocal correspondence between created queue and chunks. // Provided original schema to keep track of the original properties that gets destroied // during schema preparation => e.g. array stringification method. const queue = _makeQueue(preparedSchema, schema); - const chunks = _makeChunks(preparedString, queue); + let chunks = _makeChunks(preparedString, queue); + + if (flatten) { + chunks = chunks.map(chunk => { + chunk.pure | 0 + chunk.prevUndef | 0 + chunk.isUndef | 0 + chunk.bothUndef | 0 + return { + ...chunk, + pure: chunk.pure, + prevUndef: chunk.prevUndef, + isUndef: chunk.isUndef, + bothUndef: chunk.bothUndef, + } + }) + } + const selectChunk = _select(chunks); const { length } = queue;