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

Support TypedArrays #626

Open
2 tasks done
joshkel opened this issue Jun 1, 2023 · 1 comment
Open
2 tasks done

Support TypedArrays #626

joshkel opened this issue Jun 1, 2023 · 1 comment

Comments

@joshkel
Copy link

joshkel commented Jun 1, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

🚀 Feature Proposal

fast-json-stringify does not work with TypedArray objects (Float32Array, etc.). Normally, using JSON.stringify to serialize a TypedArray results in writing it as an object (e.g., new Float32Array([0.1,0.2]) gets serialized as {"0":0.1,"1":0.2}). However, since fast-json-stringify uses a JSON Schema to guide its output, it can know that a given property is an array; it seems reasonable and consistent to allow it to serialize TypedArrays as well as regular arrays.

Proposed implementation:

  • The Array.isArray check could be broadened to also allow TypedArrays.
  • TypedArray can only hold (floating point or integer) numbers, so it would not work with many JSON Schemas. One option would be to only allow TypedArray if the JSON Schema is compatible with TypedArray (e.g., only number items); another option would be to allow it as is and then let individual elements fail validation.
  • If a TypedArray is in use, largeArrayMechanism is ignored (treated as default).

Motivation

Our application uses TypedArray for several purposes. This is fast internally but is noticeably slower to serialize. Using fast-json-stringify, locally patched to support TypedArray, is over 2.5x faster for a Float32Array of 4000 randomly generated numbers.

Example

const array = new Float32Array(new Array(4000).fill(1));

const stringify = fastJson({
  title: 'Example Schema',
  type: 'array',
  items: {
    type: 'number'
  }
})

console.log(stringify(array));
@Eomm
Copy link
Member

Eomm commented Jun 1, 2023

Thanks for reporting!
Would you like to send a Pull Request to address this issue? Remember to add unit tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants