Inclusion on Uint8Array
syg opened this issue · 9 comments
How about also including it on Uint8Array? To me, the primary benefit would be to get encodings of subarrays without copying, since TAs are already views.
I agree on this, usually we work with Uint8Array for binary data
uint8array.buffer.toBase64() anyway should work, so why?
...
However, yes, Uint8Array.fromBase64(string) is much shorter than new Uint8Array(ArrayBuffer.fromBase64(string)).
uint8array.buffer.toBase64()anyway should work, so why?
Not necessarily, that could end up encoding more data than you want since the underlying buffer can be larger than the view.
Right, I forgot this use case.
Agreed, especially in light of #13 - you can "walk" Uint8Array views over a large ArrayBuffer to encode it piecewise, for e.g. sending over a network.
I'm not sure if it should go on Uint8Array as well as or instead of on ArrayBuffer.
Starting out with just Uint8Array makes sense to me.
It feels pretty off to me for this to live on Uint8Array specifically given (a) the existence of Uint8ClampedArray as a “sibling” class that’s no less appropriate and (b) the fact that these string codings don’t imply anything about the interpretation of the binary data. A base64 string could as readily encode variable-size-element structured data that only makes sense to read using DataView, say (a real world example of such data people do sometimes transfer using base64: fonts).
the existence of Uint8ClampedArray as a “sibling” class that’s no less appropriate
Uint8ClampedArray is definitely less appropriate - it's only for very niche cases where you want to have special behavior on assignment. Uint8Array is the canonical "sequence of bytes" typed array.
the fact that these string codings don’t imply anything about the interpretation of the binary data
Right, which is what makes Uint8Array an appropriate type - it's represents a sequence of bytes. The only difference between Uint8Array and ArrayBuffer is that Uint8Array is a view, which makes it strictly more useful - it lets you read decoded bytes directly, in those cases where that's appropriate, and it lets you encode only a part of a block of memory without doing a copy.
Closing as settled; this will be on Uint8Array.