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
SharedArrayBuffer.byteLength Property in JavaScript
The byteLength property of the SharedArrayBuffer returns an unsigned, 32-bit integer that specifies the size/length of a SharedArrayBuffer in bytes.
Syntax
sharedArrayBuffer.byteLength
Parameters
This property takes no parameters and is read-only.
Return Value
Returns the length of the SharedArrayBuffer in bytes as a 32-bit unsigned integer.
Example
<html>
<head>
<title>JavaScript Example</title>
</head>
<body>
<script type="text/javascript">
var sharedArrayBuffer = new SharedArrayBuffer(8);
var result = sharedArrayBuffer.byteLength;
document.write("Length of the shared array buffer is: " + result);
</script>
</body>
</html>
Output
Length of the shared array buffer is: 8
Multiple Buffer Sizes Example
<html>
<head>
<title>JavaScript Example</title>
</head>
<body>
<script type="text/javascript">
var buffer1 = new SharedArrayBuffer(16);
var buffer2 = new SharedArrayBuffer(1024);
var buffer3 = new SharedArrayBuffer(0);
document.write("Buffer 1 length: " + buffer1.byteLength + "<br>");
document.write("Buffer 2 length: " + buffer2.byteLength + "<br>");
document.write("Buffer 3 length: " + buffer3.byteLength);
</script>
</body>
</html>
Output
Buffer 1 length: 16 Buffer 2 length: 1024 Buffer 3 length: 0
Key Points
- The byteLength property is read-only and cannot be modified after creation
- It returns the exact number of bytes allocated when the SharedArrayBuffer was created
- The minimum value is 0 (for an empty buffer)
- The maximum value is 2^32 - 1 bytes due to the 32-bit unsigned integer limit
Conclusion
The byteLength property provides a reliable way to determine the size of a SharedArrayBuffer in bytes. This is essential for memory management and bounds checking in multi-threaded JavaScript applications.
Advertisements
