DanielHZhang/superbuffer

Question. Uint8Array instead of ArrayBuffer?

Closed this issue · 6 comments

Are you planning to add Uint8Array support as well instead of ArrayBuffer?

Do you mean that toBuffer serializes to Uint8Array and fromBuffer deserializes Uint8Array to object? Or do you mean Uint8Array as a type within schemas:

const schema = new Schema({field: [uint8]});

I mean converting JSON to Uint8Array and the opposite. For example,

const buffer = position.toUint8Array({x: 32.1, y: 12.3});

const result = position.fromUint8Array(buffer);

I know that there are ways to convert ArrayBuffer to Uint8Array. For example,

const buffer = new Uint8Array(arrayBuffer);

but this is not what I want to have in your lib.

What do you think?

Let me make sure I'm understanding this correctly. You want to avoid calling

const buffer = new Uint8Array(arrayBuffer);

in userspace because it creates a new Uint8Array instance by copying the entire ArrayBuffer. Instead, this library should handle the creation of the Uint8Array directly, instead of needing two steps via toBuffer() then new Uint8Array. Is that the gist of your suggestion?

@DanielHZhang you got it right.

Got it, thanks for the clarification. I'll implement this feature as soon as possible. However, I don't think fromUint8Array is necessary because you can access the raw ArrayBuffer with the property someUint8Array.buffer, and then just pass that to fromBuffer without extra copying.

In one of my projects I use NATS.io and payloads are Uint8Arrays. See: https://github.com/nats-io/nats.js