Similarities Between Typed and Regular Arrays
Typed arrays and regular arrays are similar in several ways, and as you’ve already seen in this chapter, typed arrays can be used like regular arrays in many situations. For instance, you can check how many elements are in a typed array using the length
property, and you can access a typed array’s elements directly using numeric indices. For example:
let ints = new Int16Array([25, 50]);
console.log(ints.length); // 2
console.log(ints[0]); // 25
console.log(ints[1]); // 50
ints[0] = 1;
ints[1] = 2;
console.log(ints[0]); // 1
console.log(ints[1]); // 2
In this code, a new Int16Array
with two items is created. The items are read from and written to using their numeric indices, and those values are automatically stored and converted into int16 values as part of the operation. The similarities don’t end there, though.
I> Unlike regular arrays, you cannot change the size of a typed array using the length
property. The length
property is not writable, so any attempt to change it is ignored in non-strict mode and throws an error in strict mode.
Common Methods
Typed arrays also include a large number of methods that are functionally equivalent to regular array methods. You can use the following array methods on typed arrays:
copyWithin()
entries()
fill()
filter()
find()
findIndex()
forEach()
indexOf()
join()
keys()
lastIndexOf()
map()
reduce()
reduceRight()
reverse()
slice()
some()
sort()
values()
Keep in mind that while these methods act like their counterparts on Array.prototype
, they are not exactly the same. The typed array methods have additional checks for numeric type safety and, when an array is returned, will return a typed array instead of a regular array (due to Symbol.species
). Here’s a simple example to demonstrate the difference:
let ints = new Int16Array([25, 50]),
mapped = ints.map(v => v * 2);
console.log(mapped.length); // 2
console.log(mapped[0]); // 50
console.log(mapped[1]); // 100
console.log(mapped instanceof Int16Array); // true
This code uses the map()
method to create a new array based on the values in ints
. The mapping function doubles each value in the array and returns a new Int16Array
.
The Same Iterators
Typed arrays have the same three iterators as regular arrays, too. Those are the entries()
method, the keys()
method, and the values()
method. That means you can use the spread operator and for-of
loops with typed arrays just like you would with regular arrays. For example:
let ints = new Int16Array([25, 50]),
intsArray = [...ints];
console.log(intsArray instanceof Array); // true
console.log(intsArray[0]); // 25
console.log(intsArray[1]); // 50
This code creates a new array called intsArray
containing the same data as the typed array ints
. As with other iterables, the spread operator makes converting typed arrays into regular arrays easy.
of() and from() Methods
Lastly, all typed arrays have static of()
and from()
methods that work like the Array.of()
and Array.from()
methods. The difference is that the methods on typed arrays return a typed array instead of a regular array. Here are some examples that use these methods to create typed arrays:
let ints = Int16Array.of(25, 50),
floats = Float32Array.from([1.5, 2.5]);
console.log(ints instanceof Int16Array); // true
console.log(floats instanceof Float32Array); // true
console.log(ints.length); // 2
console.log(ints[0]); // 25
console.log(ints[1]); // 50
console.log(floats.length); // 2
console.log(floats[0]); // 1.5
console.log(floats[1]); // 2.5
The of()
and from()
methods in this example are used to create an Int16Array
and a Float32Array
, respectively. These methods ensure that typed arrays can be created just as easily as regular arrays.