Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
TypedArray.byteLength property in JavaScript
The byteLength property of the TypedArray object represents the length of the TypedArray in bytes. It is a read-only property that returns the size of the underlying buffer used by the typed array.
Syntax
typedArray.byteLength
Example: Basic Usage
<html>
<head>
<title>JavaScript Example</title>
</head>
<body>
<script type="text/javascript">
var buffer = new ArrayBuffer(156);
var float32 = new Float32Array(buffer);
document.write("ByteLength: " + float32.byteLength);
</script>
</body>
</html>
Output
ByteLength: 156
Example: Different TypedArray Types
<html>
<head>
<title>TypedArray ByteLength Examples</title>
</head>
<body>
<script type="text/javascript">
// Create different typed arrays
var int8Array = new Int8Array(10); // 1 byte per element
var int16Array = new Int16Array(10); // 2 bytes per element
var int32Array = new Int32Array(10); // 4 bytes per element
var float64Array = new Float64Array(10); // 8 bytes per element
document.write("Int8Array byteLength: " + int8Array.byteLength + "<br>");
document.write("Int16Array byteLength: " + int16Array.byteLength + "<br>");
document.write("Int32Array byteLength: " + int32Array.byteLength + "<br>");
document.write("Float64Array byteLength: " + float64Array.byteLength + "<br>");
</script>
</body>
</html>
Output
Int8Array byteLength: 10 Int16Array byteLength: 20 Int32Array byteLength: 40 Float64Array byteLength: 80
Comparison with length Property
| Property | Returns | Example (Int32Array with 5 elements) |
|---|---|---|
length |
Number of elements | 5 |
byteLength |
Size in bytes | 20 (5 × 4 bytes) |
Key Points
- The
byteLengthproperty is read-only - It returns the total memory size occupied by the typed array
- The value equals
length × BYTES_PER_ELEMENT - Different typed array types have different byte sizes per element
Conclusion
The byteLength property provides the total memory footprint of a TypedArray in bytes. It's useful for memory management and understanding the storage requirements of different typed array types.
Advertisements
