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.buffer property in JavaScript
The buffer property of TypedArray instances provides access to the underlying ArrayBuffer object that stores the binary data for the typed array.
Syntax
typedArray.buffer
Return Value
Returns the ArrayBuffer object that backs the TypedArray instance.
Example
<html>
<head>
<title>JavaScript Example</title>
</head>
<body>
<script type="text/javascript">
var buffer = new ArrayBuffer(156);
var float32 = new Float32Array(buffer);
document.write("Buffer byte length: " + float32.buffer.byteLength);
</script>
</body>
</html>
Output
Buffer byte length: 156
Multiple TypedArrays Sharing Buffer
<html>
<head>
<title>Shared Buffer Example</title>
</head>
<body>
<script type="text/javascript">
// Create a buffer
var sharedBuffer = new ArrayBuffer(16);
// Create different typed arrays using the same buffer
var int8View = new Int8Array(sharedBuffer);
var int32View = new Int32Array(sharedBuffer);
// Both arrays share the same buffer
document.write("Same buffer: " + (int8View.buffer === int32View.buffer) + "<br>");
document.write("Buffer length: " + int8View.buffer.byteLength + " bytes");
</script>
</body>
</html>
Output
Same buffer: true Buffer length: 16 bytes
Key Points
- The
bufferproperty is read-only - Multiple TypedArrays can share the same underlying ArrayBuffer
- Changes to the buffer through one TypedArray are visible in others that share it
- Useful for memory-efficient operations and data sharing between different views
Conclusion
The buffer property provides access to the underlying ArrayBuffer, enabling memory sharing between different TypedArray views and efficient binary data manipulation.
Advertisements
