lucagez / lucagez/slow-json-stringify

Optimization for plain objects

Open
#28 1 comment 2 reactions 0 assignees View on GitHub
enhancement help wanted
Dominant language
JavaScript
Stars
474
Forks
16
PR merge metrics
No merged PRs in 30d

Description

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:

```js
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, )` can be used to traverse all the properties of `value` and building a [schema](https://github.com/lucagez/slow-json-stringify#defining-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

Contributor guide

No contributing guide indexed for this repository

Research direction

No files or tests are named. Start with the custom serializer path illustrated by attr('null', ...), then review how JSON.stringify and schema-based serialization are currently connected. Done would require a defined approach for unknown object shapes, schema creation, and schema invalidation when shapes change.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.