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

unexpected Uint8Array.prototype.buffer behavior #145

Closed
septs opened this issue Oct 20, 2020 · 2 comments
Closed

unexpected Uint8Array.prototype.buffer behavior #145

septs opened this issue Oct 20, 2020 · 2 comments

Comments

@septs
Copy link

septs commented Oct 20, 2020

buf = new Uint8Array(2048)
buf.subarray(0, 670).buffer
// ArrayBuffer(2048)
buf.slice(0, 670).buffer
// ArrayBuffer(670)

private getUint8Array(): Uint8Array {
return this.bytes.subarray(0, this.pos);
}

References:
DimensionDev/Maskbook#1573

@Jack-Works
Copy link

slice might copy the buffer then affect the performance

@gfx
Copy link
Member

gfx commented Oct 22, 2020

As @Jack-Works said, it is intentional.

It's not specific to this library. In JavaScript and TypeScript, You MUST NOT use the underlying buffer in typed arrays without offset and length. Typed arrays and DataView are a view to ArrayBuffer. In addition, I don't recommend to copy typed arrays and their underlying buffers unless they are mutable. Copying buffers affects the performance.

@gfx gfx closed this as completed Oct 22, 2020
@gfx gfx mentioned this issue Oct 22, 2020
nmargaritis added a commit to skgdev/socket.io-msgpack-javascript that referenced this issue Jun 16, 2021
Normally msgpack-javascript should work out of the box as the output of encode is Uint8array but due to this issue we have to initialise it again as the buffer is missing Ref msgpack/msgpack-javascript#145
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

3 participants