-
Notifications
You must be signed in to change notification settings - Fork 58
Accept a Uint8Array so as not to waste memory and time? #22
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
Comments
Where does the copy appear? The following code doesn't copy the array content: const array = new Uint8Array([45, 69, 80]);
const buffer = array.buffer;
const uintView = new Uint8Array(buffer); Or are you talking about memory and time spending on creating a |
Here's an example
now I only want to pass a portion of There's no way to do it. I want to pass in a
But the current API requires I copy that to a new arraybuffer I want to do this
but I can't. I also can't do this
So I'm forced to make a copy
I want to avoid that copy |
It makes sense, thanks. But I'd suggest a more universal solution: function getUint8Array(bufferOrView) {
if (bufferOrView instanceof ArrayBuffer) {
return new Uint8Array(bufferOrView);
}
if (ArrayBuffer.isView(bufferOrView)) {
return new Uint8Array(bufferOrView.buffer, bufferOrView.byteOffset, bufferOrView.byteLength);
}
throw new TypeError('Unsupported argument');
}
exports.encode = function(arraybuffer) {
var bytes = getUint8Array(arraybuffer);
// ...
} It can bloat the code too much. This solution would be suitable too: exports.encode = function(arraybuffer, byteOffset, byteLength) {
var bytes = new Uint8Array(arraybuffer, byteOffset, byteLength);
// ...
} Or this: exports.encode = function(bytes) {
// Can be presented only in the type declarations
if (!(bytes instanceof Uint8Array || bytes instanceof Uint8ClampArray)) {
throw new TypeError('Unsupported argument');
}
// ...
} |
It sounds good to me. But I'm not a maintainer of this repository, so I can't change the code.
Nice notice, I've updated my example, thanks |
As it is, if you have some Uint8Array that is a view into a larger ArrayBuffer then a copy of the data is made in encode because
new Uint8Array(someOtherUint8Array)
makes new ArrayBuffer with a copy of the data. Where as you could just use the Uint8Array passed in?Basically this would change
To something like
The text was updated successfully, but these errors were encountered: