fast-json-stringify
Advanced tools
Comparing version 5.4.0 to 5.4.1
13
index.js
@@ -59,7 +59,3 @@ 'use strict' | ||
if (location.isValidated) { | ||
validatorSchemasIds.add(schemaId) | ||
} | ||
const newLocation = new Location(schema, schemaId, jsonPointer, location.isValidated) | ||
const newLocation = new Location(schema, schemaId, jsonPointer) | ||
if (schema.$ref !== undefined) { | ||
@@ -141,2 +137,7 @@ return resolveRef(newLocation, schema.$ref) | ||
validator.addSchema(schema, schemaId) | ||
const dependencies = refResolver.getSchemaDependencies(schemaId) | ||
for (const [schemaId, schema] of Object.entries(dependencies)) { | ||
validator.addSchema(schema, schemaId) | ||
} | ||
} | ||
@@ -459,3 +460,2 @@ | ||
function addIfThenElse (location, input) { | ||
location.isValidated = true | ||
validatorSchemasIds.add(location.getSchemaId()) | ||
@@ -846,3 +846,2 @@ | ||
if (type === undefined && (schema.anyOf || schema.oneOf)) { | ||
location.isValidated = true | ||
validatorSchemasIds.add(location.getSchemaId()) | ||
@@ -849,0 +848,0 @@ |
'use strict' | ||
class Location { | ||
constructor (schema, schemaId, jsonPointer = '#', isValidated = false) { | ||
constructor (schema, schemaId, jsonPointer = '#') { | ||
this.schema = schema | ||
this.schemaId = schemaId | ||
this.jsonPointer = jsonPointer | ||
this.isValidated = isValidated | ||
this.mergedSchemaId = null | ||
@@ -16,4 +15,3 @@ } | ||
this.schemaId, | ||
this.jsonPointer + '/' + propertyName, | ||
this.isValidated | ||
this.jsonPointer + '/' + propertyName | ||
) | ||
@@ -20,0 +18,0 @@ |
@@ -31,2 +31,14 @@ 'use strict' | ||
getSchemaDependencies (schemaId, dependencies = {}) { | ||
const schema = this.schemas[schemaId] | ||
for (const dependencySchemaId of schema.dependencies) { | ||
if (dependencies[dependencySchemaId] !== undefined) continue | ||
dependencies[dependencySchemaId] = this.getSchema(dependencySchemaId) | ||
this.getSchemaDependencies(dependencySchemaId, dependencies) | ||
} | ||
return dependencies | ||
} | ||
insertSchemaBySchemaId (schema, schemaId) { | ||
@@ -39,3 +51,3 @@ if ( | ||
} | ||
this.schemas[schemaId] = { schema, anchors: {} } | ||
this.schemas[schemaId] = { schema, anchors: {}, dependencies: [] } | ||
} | ||
@@ -65,2 +77,10 @@ | ||
const ref = schema.$ref | ||
if (ref !== undefined && typeof ref === 'string') { | ||
if (ref.charAt(0) !== '#') { | ||
const dependencySchemaId = ref.split('#')[0] | ||
this.schemas[rootSchemaId].dependencies.push(dependencySchemaId) | ||
} | ||
} | ||
for (const key in schema) { | ||
@@ -67,0 +87,0 @@ if (typeof schema[key] === 'object' && schema[key] !== null) { |
'use strict' | ||
// eslint-disable-next-line | ||
const STR_ESCAPE = /[\u0000-\u001f\u0022\u005c\ud800-\udfff]|[\ud800-\udbff](?![\udc00-\udfff])|(?:[^\ud800-\udbff]|^)[\udc00-\udfff]/ | ||
module.exports = class Serializer { | ||
@@ -97,2 +100,7 @@ constructor (options = {}) { | ||
// Fast escape chars check | ||
if (!STR_ESCAPE.test(str)) { | ||
return quotes + str + quotes | ||
} | ||
if (str.length < 42) { | ||
@@ -99,0 +107,0 @@ return this.asStringSmall(str) |
{ | ||
"name": "fast-json-stringify", | ||
"version": "5.4.0", | ||
"version": "5.4.1", | ||
"description": "Stringify your JSON at max speed", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"scripts": { | ||
"bench": "node ./benchmark/bench.js", | ||
"bench:cmp": "node ./benchmark/bench-cmp-branch.js", | ||
"bench:cmp:ci": "node ./benchmark/bench-cmp-branch.js --ci", | ||
"benchmark": "node ./benchmark/bench-cmp-lib.js", | ||
"lint": "standard", | ||
"lint:fix": "standard --fix", | ||
"test:typescript": "tsc --project ./test/types/tsconfig.json && tsd", | ||
"test:unit": "tap -J test/*.test.js test/**/*.test.js", | ||
"test": "npm run test:unit && npm run test:typescript" | ||
}, | ||
"precommit": ["lint", "test"], | ||
"precommit": [ | ||
"lint", | ||
"test" | ||
], | ||
"repository": { | ||
@@ -37,3 +29,3 @@ "type": "git", | ||
"@fastify/pre-commit": "^2.0.2", | ||
"@sinclair/typebox": "^0.24.9", | ||
"@sinclair/typebox": "^0.25.2", | ||
"benchmark": "^2.1.4", | ||
@@ -69,3 +61,14 @@ "cli-select": "^1.1.2", | ||
"directory": "test/types" | ||
}, | ||
"scripts": { | ||
"bench": "node ./benchmark/bench.js", | ||
"bench:cmp": "node ./benchmark/bench-cmp-branch.js", | ||
"bench:cmp:ci": "node ./benchmark/bench-cmp-branch.js --ci", | ||
"benchmark": "node ./benchmark/bench-cmp-lib.js", | ||
"lint": "standard", | ||
"lint:fix": "standard --fix", | ||
"test:typescript": "tsc --project ./test/types/tsconfig.json && tsd", | ||
"test:unit": "tap -J test/*.test.js test/**/*.test.js", | ||
"test": "npm run test:unit && npm run test:typescript" | ||
} | ||
} | ||
} |
@@ -21,27 +21,33 @@ # fast-json-stringify | ||
- Machine: `EX41S-SSD, Intel Core i7, 4Ghz, 64GB RAM, 4C/8T, SSD`. | ||
- Node.js `v16.9.1` | ||
- Node.js `v18.12.1` | ||
``` | ||
FJS creation x 8,443 ops/sec ±1.01% (90 runs sampled) | ||
CJS creation x 183,219 ops/sec ±0.13% (96 runs sampled) | ||
AJV Serialize creation x 83,541,848 ops/sec ±0.24% (98 runs sampled) | ||
JSON.stringify array x 5,363 ops/sec ±0.11% (100 runs sampled) | ||
fast-json-stringify array x 6,747 ops/sec ±0.13% (98 runs sampled) | ||
compile-json-stringify array x 7,121 ops/sec ±0.42% (98 runs sampled) | ||
AJV Serialize array x 7,533 ops/sec ±0.13% (98 runs sampled) | ||
JSON.stringify long string x 16,461 ops/sec ±0.12% (98 runs sampled) | ||
fast-json-stringify long string x 16,443 ops/sec ±0.37% (99 runs sampled) | ||
compile-json-stringify long string x 16,458 ops/sec ±0.09% (98 runs sampled) | ||
AJV Serialize long string x 21,433 ops/sec ±0.08% (95 runs sampled) | ||
JSON.stringify short string x 12,035,664 ops/sec ±0.62% (96 runs sampled) | ||
fast-json-stringify short string x 38,281,060 ops/sec ±0.24% (98 runs sampled) | ||
compile-json-stringify short string x 32,388,037 ops/sec ±0.27% (97 runs sampled) | ||
AJV Serialize short string x 32,288,612 ops/sec ±0.32% (95 runs sampled) | ||
JSON.stringify obj x 3,068,185 ops/sec ±0.16% (98 runs sampled) | ||
fast-json-stringify obj x 10,082,694 ops/sec ±0.10% (97 runs sampled) | ||
compile-json-stringify obj x 17,037,963 ops/sec ±1.17% (97 runs sampled) | ||
AJV Serialize obj x 9,660,041 ops/sec ±0.11% (97 runs sampled) | ||
JSON stringify date x 1,084,008 ops/sec ±0.16% (98 runs sampled) | ||
fast-json-stringify date format x 1,781,044 ops/sec ±0.48% (99 runs sampled) | ||
compile-json-stringify date format x 1,086,187 ops/sec ±0.16% (99 runs sampled) | ||
FJS creation x 4,129 ops/sec ±0.82% (92 runs sampled) | ||
CJS creation x 184,196 ops/sec ±0.12% (97 runs sampled) | ||
AJV Serialize creation x 61,130,591 ops/sec ±0.40% (92 runs sampled) | ||
JSON.stringify array x 5,057 ops/sec ±0.10% (100 runs sampled) | ||
fast-json-stringify array default x 6,243 ops/sec ±0.14% (98 runs sampled) | ||
fast-json-stringify array json-stringify x 6,261 ops/sec ±0.30% (99 runs sampled) | ||
compile-json-stringify array x 6,842 ops/sec ±0.18% (96 runs sampled) | ||
AJV Serialize array x 6,964 ops/sec ±0.11% (95 runs sampled) | ||
JSON.stringify large array x 248 ops/sec ±0.07% (90 runs sampled) | ||
fast-json-stringify large array default x 99.96 ops/sec ±0.22% (74 runs sampled) | ||
fast-json-stringify large array json-stringify x 248 ops/sec ±0.07% (90 runs sampled) | ||
compile-json-stringify large array x 317 ops/sec ±0.09% (89 runs sampled) | ||
AJV Serialize large array x 111 ops/sec ±0.07% (33 runs sampled) | ||
JSON.stringify long string x 16,002 ops/sec ±0.09% (98 runs sampled) | ||
fast-json-stringify long string x 15,979 ops/sec ±0.09% (96 runs sampled) | ||
compile-json-stringify long string x 15,952 ops/sec ±0.31% (97 runs sampled) | ||
AJV Serialize long string x 21,416 ops/sec ±0.08% (98 runs sampled) | ||
JSON.stringify short string x 12,944,272 ops/sec ±0.09% (96 runs sampled) | ||
fast-json-stringify short string x 30,585,790 ops/sec ±0.27% (97 runs sampled) | ||
compile-json-stringify short string x 30,656,406 ops/sec ±0.12% (96 runs sampled) | ||
AJV Serialize short string x 30,406,785 ops/sec ±0.37% (96 runs sampled) | ||
JSON.stringify obj x 3,153,043 ops/sec ±0.33% (99 runs sampled) | ||
fast-json-stringify obj x 6,866,434 ops/sec ±0.11% (100 runs sampled) | ||
compile-json-stringify obj x 15,886,723 ops/sec ±0.15% (98 runs sampled) | ||
AJV Serialize obj x 8,969,043 ops/sec ±0.36% (97 runs sampled) | ||
JSON stringify date x 1,126,547 ops/sec ±0.09% (97 runs sampled) | ||
fast-json-stringify date format x 1,836,188 ops/sec ±0.12% (99 runs sampled) | ||
compile-json-stringify date format x 1,125,735 ops/sec ±0.19% (98 runs sampled) | ||
``` | ||
@@ -48,0 +54,0 @@ |
@@ -619,1 +619,29 @@ 'use strict' | ||
}) | ||
test('object with ref and validated properties', (t) => { | ||
t.plan(1) | ||
const externalSchemas = { | ||
RefSchema: { | ||
$id: 'RefSchema', | ||
type: 'string' | ||
} | ||
} | ||
const schema = { | ||
$id: 'root', | ||
type: 'object', | ||
properties: { | ||
id: { | ||
anyOf: [ | ||
{ type: 'string' }, | ||
{ type: 'number' } | ||
] | ||
}, | ||
reference: { $ref: 'RefSchema' } | ||
} | ||
} | ||
const stringify = build(schema, { schema: externalSchemas }) | ||
t.equal(stringify({ id: 1, reference: 'hi' }), '{"id":1,"reference":"hi"}') | ||
}) |
334930
12476
717