Why does Blob()
yield different results between passing an Array and a Uint8Array, when all values represent raw bytes?
new Blob([[0,60,0,60]])
// Blob {size: 9, type: ''}
new Blob([new Uint8Array([0,60,0,60])])
// Blob {size: 4, type: ''}
Why does Blob()
yield different results between passing an Array and a Uint8Array, when all values represent raw bytes?
new Blob([[0,60,0,60]])
// Blob {size: 9, type: ''}
new Blob([new Uint8Array([0,60,0,60])])
// Blob {size: 4, type: ''}
2
Answers
TL;DR
Array
of numbers ([[0,60,0,60]]
) to theBlob
constructor converts them to strings."0,60,0,60"
size = 9Uint8Array
(new Uint8Array([0,60,0,60])
) to theBlob
constructor results in an internalBufferArray
that represents the givenBytes
.[0, 60, 0, 60]
size = 4Explanation
The first Blob constructor parameters (W3) (
blobParts
) only support three different types:Input breakdown (Array)
[]
is theblobParts
sequence.[0, 60, 0, 60]
is a regular JavaScriptarray
ofnumbers
.BufferSource
,Blob
, nor aUSVString
. When the constructor encounters an unsupported type, it converts it to a string using the default toString() method (read 3 and 4).[0, 60, 0, 60].toString()
results in the string"0,60,0,60"
."0,60,0,60"
is treated as aUSVString
, encoded inUTF-8
.Input breakdown (Uint8Array)
[new Uint8Array([0, 60, 0, 60])]
is theblobParts
sequence.Uint8Array
is aBufferSource
.[0, 60, 0, 60]
results in 4 bytes.The
Blob
constructor does not take an array of arrays as its argument. It takes an array of binary values – those might be buffers, blobs, typed arrays, or strings, but not arrays. The array you are passing is converted to a string, whose 9 characters are put as bytes into the blob: