Categories
Modern JavaScript

Best of Modern JavaScript — Typed Array Methods

Spread the love

Since 2015, JavaScript has improved immensely.

It’s much more pleasant to use it now than ever.

In this article, we’ll look at JavaScript typed arrays methods.

Static Methods

The from static method lets us convert an array-like or iterable object into a typed array.

The first argument is the iterable object we want to convert to a typed array.

The 2nd is a callback that has the value of the entry and the index as the parameter of the array to let us return something we want.

The 3rd argument is the value of this we want to have in the callback in the 2nd argument.

The 2nd and 3rd arguments are optional.

For example, we can write:

const typedArr = Uint16Array.from([0, 1, 2]);

We pass in the array value and returns a Uint16Array with the entries from the array.

The callback for mapping the entries let us create a new array that doesn’t overflow rather than have them overflow.

For example, instead of writing:

const arr = Int8Array.of(200, 201, 202).map(x => 2 * x)

And get the Int8Array with values [-112, -110, -108] . We can write:

const arr = Int32Array.from(Int8Array.of(120, 121, 122), x => x * 2)

and get [240, 242, 244] in an Int32Array .

Instance Properties

There are many instance properties included with typed arrays.

The buffer property is a getter than returns the buffer that’s storing the data of the typed array.

The byteLength property is a number that returns the size in bytes of the typed array’s buffer.

byteOffset returns the offset where the typed array starts inside the ArrayByffr.

set copies all elements of an array or typed array into the given typed array.

If it’s a normal array, then all elements are converted to numbers.

If the argument is a typed array, then each element is converted to the type appropriate for the typed array.

subarray returns a new typed array that has the same buffer as the typed array it’s called on.

It takes the beginning index as the first argument. The default value of this is 0.

The end index is its second argument. The default value of this is the length of the array.

The values can be negative.

Array Methods

Array methods like copyWithin , entries , every , fill , filter , find , findIndex , forEach , indexOf , join , keys , lastIndexOf , length , map , reduce , reduceRight , reverse , slice , some , sort , toLocaleString , toString , and values are all available to typed arrays.

They take the same arguments and have the same return values as regular array methods.

Constructor

Typed arrays constructors include Int8Array, Uint8Array, Uint8ClampedArray , Int16Array, Uint16Array, Int32Array, Uint32Array, Float32Array, Float64Array.

They can have the signature (buffer, byteOffset=0, length?) , (length) , (typedArray) ,(arrayLikeObject) , or () .

buffer is the buffer of a typed array.

ByteOffset is the offset from index 0.

length is the length of the typed array.

typedArray is another typed array.

arrayLikeObject is an array-like or iterable object.

An array-like object is one with the length property and integer keys.

Conclusion

Typed arrays have many array methods.

The constructor is different from the regular Array constructor.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.