This allows us to be more efficient as it specifies how much amount of memory is to be utilized and also helps in the transfer of binary data in the expected format.
Uses of TypedArrays: If we want to transfer binary data as a 16-bit contiguous integer format rather than a conventional 64-bit floating-point, TypedArrays will be efficient. In simpler words, with TypedArrays, we are creating arrays with their content strictly controlled.
Example 1: Let’s say you wanted to render some 3d graphics on the web. This requires being able to transfer an array of numbers, say pixel values as RGB, or some data to a native interface, which needed it to be in some specific size(for RGBA in (0,255) ). But for TypedArrays, we would have implemented a lot of data handling to make sure things work properly, and that too in 64-bit floating-point format.
Clamped PixelData: 143,255,255,255,182,64,19,255,183,194
Explanation: The above output shows how easily pixel values are clamped achieved using Uint8ClampedArray without requiring any explicit data handling.
Example 2: In this example, we will see how we can attach typed array views and DataViews to the buffer to reads the contents of the specified file. Many API’s make use of TypedArrays such as FileReader.prototype.readAsArrayBuffer() . The FileReader can read file contents as an ArrayBuffer.
Explanation: The above example shows us how with the help of FileReader API and TypedArray views, we can view the contents of the file or blob.