Giter Club home page Giter Club logo

Comments (3)

mcollina avatar mcollina commented on June 8, 2024 2

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.

anuj-kr-jha avatar anuj-kr-jha commented on June 8, 2024 1

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.

sailingwithsandeep avatar sailingwithsandeep commented on June 8, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.