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:

  1. let ints = new Int16Array([25, 50]);
  2. console.log(ints.length); // 2
  3. console.log(ints[0]); // 25
  4. console.log(ints[1]); // 50
  5. ints[0] = 1;
  6. ints[1] = 2;
  7. console.log(ints[0]); // 1
  8. 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:

  1. let ints = new Int16Array([25, 50]),
  2. mapped = ints.map(v => v * 2);
  3. console.log(mapped.length); // 2
  4. console.log(mapped[0]); // 50
  5. console.log(mapped[1]); // 100
  6. 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:

  1. let ints = new Int16Array([25, 50]),
  2. intsArray = [...ints];
  3. console.log(intsArray instanceof Array); // true
  4. console.log(intsArray[0]); // 25
  5. 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:

  1. let ints = Int16Array.of(25, 50),
  2. floats = Float32Array.from([1.5, 2.5]);
  3. console.log(ints instanceof Int16Array); // true
  4. console.log(floats instanceof Float32Array); // true
  5. console.log(ints.length); // 2
  6. console.log(ints[0]); // 25
  7. console.log(ints[1]); // 50
  8. console.log(floats.length); // 2
  9. console.log(floats[0]); // 1.5
  10. 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.