How to store a byte array in Javascript
By using typed arrays, you can store arrays of these types:
Type | Value Range | Size(bytes) |
---|---|---|
Int8Array |
-128 to 127 | 1 |
Uint8Array |
0 to 255 | 1 |
Uint8ClampedArray |
0 to 255 | 1 |
Int16Array |
-32768 to 32767 | 2 |
Uint16Array |
0 to 65535 | 2 |
Int32Array |
-2147483648 to 2147483647 | 4 |
Uint32Array |
0 to 4294967295 | 4 |
Float32Array |
-3.4E38 to 3.4E38 | 4 |
Float64Array |
-1.8E308 to 1.8E308 | 8 |
BigInt64Array |
-2^63 to 2^63 - 1 | 8 |
BigUint64Array |
0 to 2^64 - 1 | 8 |
Demo in Stack Snippets & JSFiddle
var array = new Uint8Array(100);
array[42] = 10;
console.log(array[42]);
var array = new Uint8Array(100);
array[10] = 256;
array[10] === 0 // true
I verified in firefox and chrome, its really an array of bytes :
var array = new Uint8Array(1024*1024*50); // allocates 50MBytes
You could store the data in an array of strings of some large fixed size. It should be efficient to access any particular character in that array of strings, and to treat that character as a byte.
It would be interesting to see the operations you want to support, perhaps expressed as an interface, to make the question more concrete.