11
I was reading on MDN about Typedarray and saw that various classes derive from this.
Classes derived from TypeArray
:
- Int8array
- Uint8array
- Uint8clampedarray
- Int16array
- Uint16array
- Int32array
- Uint32array
- Float32array
- Float64array
From what I understand, each of these "arrays" accept a different type of input.
A test with Int8Array
:
var arr = new Int8Array(4);
arr[0] = 1;
arr[1] = 2;
arr[2] = 2.5;
arr[3] = "X2.5";
console.log(arr, arr.constructor.name);
In the above case, 2.5
as the value string
, were converted to int
.
Bearing in mind that these arrays
have this kind of behavior, I’d like to ask some questions:
In which cases should I use the
TypedArray
instead ofArray
Javascript standard? Would you like examplesIs there any performance gain when using a
TypedArray
instead of using theArray
?I would like to know about compatibility and polyfills on the same.
Another difference is initialization. Comparing for example
new Int8Array(4)
tonew Array(4)
where in the latter the elements are all asundefined
. Just as they also define the size of each element of the array in bytes– Isac
Thanks for the remark, @Isac.
– Wallace Maxters