Comments (3)
Good spot. There is a difference in how we render regexp:
import fastJson from './index.js';
const stringify = fastJson({
title: 'Example Schema',
type: 'object',
properties: {
firstName: {
type: 'string',
},
lastName: {
type: 'string',
},
age: {
description: 'Age in years',
type: 'integer',
},
reg: {
type: 'string',
},
},
});
console.log(stringify({
firstName: 'Matteo',
lastName: 'Collina',
age: 32,
reg: /"([^"]|\\")*"/,
}))t
console.log(JSON.stringify({
firstName: 'Matteo',
lastName: 'Collina',
age: 32,
reg: /"([^"]|\\")*"/,
}))
As you can see the output is different:
{"firstName":"Matteo","lastName":"Collina","age":32,"reg":"\"([^\"]|\\\\\")*\""}
{"firstName":"Matteo","lastName":"Collina","age":32,"reg":{}}
The former takes more time to compute. Note that you are telling fjs you want a string, and it's coercing the regexp into it. Instead, JSON.stringify()
returns an empty object. It's not a fair comparison.
Note that if you want to benchmark this you should run it in a tight loop, otherwise none of the optimizations will kick in - if you never call fjs, it won't get faster. Here is a good way to benchmark:
import fastJson from './index.js';
const stringify = fastJson({
title: 'Example Schema',
type: 'object',
properties: {
firstName: {
type: 'string',
},
lastName: {
type: 'string',
},
age: {
description: 'Age in years',
type: 'integer',
},
reg: {
type: 'string',
},
},
});
console.time('fast-json-stringify');
for (let i = 0; i < 1000000; i++) {
stringify({
firstName: 'Matteo',
lastName: 'Collina',
age: 32
})
}
console.timeEnd('fast-json-stringify');
console.time('JSON.stringify');
for (let i = 0; i < 1000000; i++) {
JSON.stringify({
firstName: 'Matteo',
lastName: 'Collina',
age: 32
})
}
console.timeEnd('JSON.stringify');
from fast-json-stringify.
Yes, I think optimizer kicks in when the serializer gets warm.
I checked it on container and observed that it is ~3x-4x faster than JSON.stringify after some times.
Although JSON.stringify seems faster initially.
from fast-json-stringify.
Thanks for the heads up.
Also, after running it for a while (maybe because of memorization?), I got the improved result from my benchmark as well.
Anyways, Sorry for being skeptical here!!!
from fast-json-stringify.
Related Issues (20)
- Recursive validation schema compiles but fails at runtime HOT 5
- error while resolving nested objects HOT 2
- `node:crypto` issues in Next.js HOT 7
- Tuple of date-time does not allow Date HOT 3
- Issue with custom keyword not working in fast-json-stringify HOT 11
- Response schema compiler generated validation code does not check for null values on optional object fields HOT 9
- lack of strictSchema in ajv options results in silent typos
- merged schema cache is messed up HOT 2
- emit CJS/ESM syntax in standalone mode based on AJV code options HOT 1
- When `asString` receives `undefined` an exception is thrown HOT 2
- Date coercion isn't supported by TypeScript types HOT 4
- allOf Schema and additionalProperties result in unexpected result HOT 10
- asStringSmall seem slower than JSON.stringify
- Release a new version with "merged" performance improvements HOT 2
- chunking/Readable stringify HOT 4
- Support of JSON Schema draft 8 (2019-09) HOT 3
- Date type is giving error - Error: schema is invalid: data/properties/updated_at/type must be equal to one of the allowed values HOT 4
- Release new version with improved performance
- JSON schema with single quote in property name fails to be processed when the property is required
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fast-json-stringify.