Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimization for plain objects #28

Open
lucagez opened this issue May 11, 2021 · 1 comment
Open

Optimization for plain objects #28

lucagez opened this issue May 11, 2021 · 1 comment
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@lucagez
Copy link
Owner

lucagez commented May 11, 2021

There's currently no support for serializing plain objects with unknown shape. A temporary workaround for this is to use a custom serializer, specifying null as a target type. Naively:

attr('null', (value) => JSON.stringify(value))

It could be possible to optimize this kind of scenario with a jit like approach:

  1. A custom helper can handle the serialization of Objects with unknown shape
  2. During the first serialization, JSON.stringify(value, <custom-replacer>) can be used to traverse all the properties of value and building a schema
  3. The JSON.stringify serializer can then be replaced with the schema built in step 2

Caveats

Shape consistency:
This jit like behavior would have an appreciable performance benefit if the unknown shape stays consistent for enough time to justify the performance penalty of building the schema.

Schema invalidation:
A check should be performed on the incoming object to make sure its shape matches the one of the generated schema. Otherwise, the schema should be invalidated and recreated. This step could have a non trivial performance impact

Any idea/suggestion welcome

@lucagez lucagez added enhancement New feature or request help wanted Extra attention is needed labels May 11, 2021
@dalisoft
Copy link

@lucagez Thanks.
if you want run benchmark on your machine, look at https://github.com/nanoexpress/json-serialize/blob/master/bench-suite.js.

On main benchmark your library wins, but in Suite on gets a little less performance score

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants