TypedArray.BYTES_PER_ELEMENT property in JavaScript

The BYTES_PER_ELEMENT property of TypedArrays represents the number of bytes each element occupies in memory. This static property helps determine the memory size of different typed array types.

Syntax

TypedArray.BYTES_PER_ELEMENT

Where TypedArray can be any typed array constructor like Int8Array, Float32Array, etc.

Example: Different TypedArray Sizes

<html>
<head>
    <title>JavaScript Example</title>
</head>
<body>
    <script type="text/javascript">
        var sizeOfFloat32Array = Float32Array.BYTES_PER_ELEMENT;
        document.write("Size of Float32Array element: " + sizeOfFloat32Array + " bytes");
        document.write("<br>");
        
        var sizeOfInt16Array = Int16Array.BYTES_PER_ELEMENT;
        document.write("Size of Int16Array element: " + sizeOfInt16Array + " bytes");
        document.write("<br>");
        
        var sizeOfInt8Array = Int8Array.BYTES_PER_ELEMENT;
        document.write("Size of Int8Array element: " + sizeOfInt8Array + " bytes");
        document.write("<br>");
        
        var sizeOfFloat64Array = Float64Array.BYTES_PER_ELEMENT;
        document.write("Size of Float64Array element: " + sizeOfFloat64Array + " bytes");
    </script>
</body>
</html>

Output

Size of Float32Array element: 4 bytes
Size of Int16Array element: 2 bytes
Size of Int8Array element: 1 bytes
Size of Float64Array element: 8 bytes

Common TypedArray Byte Sizes

TypedArray BYTES_PER_ELEMENT Value Range
Int8Array 1 -128 to 127
Uint8Array 1 0 to 255
Int16Array 2 -32,768 to 32,767
Uint16Array 2 0 to 65,535
Int32Array 4 -2,147,483,648 to 2,147,483,647
Float32Array 4 32-bit floating point
Float64Array 8 64-bit floating point

Practical Use Case

You can use BYTES_PER_ELEMENT to calculate the total memory usage of a typed array:

<html>
<head>
    <title>Memory Calculation</title>
</head>
<body>
    <script type="text/javascript">
        var arr = new Float32Array(1000);
        var totalBytes = arr.length * Float32Array.BYTES_PER_ELEMENT;
        document.write("Array length: " + arr.length + " elements<br>");
        document.write("Bytes per element: " + Float32Array.BYTES_PER_ELEMENT + "<br>");
        document.write("Total memory usage: " + totalBytes + " bytes");
    </script>
</body>
</html>

Output

Array length: 1000 elements
Bytes per element: 4
Total memory usage: 4000 bytes

Conclusion

The BYTES_PER_ELEMENT property is essential for memory calculations and understanding the storage requirements of different typed arrays. It helps optimize memory usage in performance-critical applications.

Updated on: 2026-03-15T23:18:59+05:30

160 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements