Sub topic 15
Growable SharedArrayBuffers
This chapter assumes you know how to create a SharedArrayBuffer, put typed array views on it, and send it to a worker. It also assumes you know the basic Atomics operations from earlier chapters.
A plain SharedArrayBuffer has a fixed size, and you pick that size when you create it. If you later need more room, you have to allocate a bigger buffer, copy the data, and get every thread to switch to the new buffer. That switch is hard to coordinate safely. A growable SharedArrayBuffer (added in ES2024) lets the existing buffer grow in place, so every thread keeps using the same object.
Creating a Growable Buffer
Pass a maxByteLength option to the constructor. The first argument is the starting size, and maxByteLength is the upper limit.
const sab = new SharedArrayBuffer(8, { maxByteLength: 64 });
console.log(sab.byteLength); // 8
console.log(sab.growable); // true
console.log(sab.maxByteLength); // 64Call grow(newByteLength) to enlarge the buffer. The new bytes start as zero. A growable shared buffer can never shrink, and this rule is deliberate: if one thread could shrink the buffer, another thread's valid index could suddenly point past the end.
| Call (current size 32, max 64) | Result |
|---|---|
| `sab.grow(48)` | Grows to 48 bytes |
| `sab.grow(32)` | Allowed, nothing changes |
| `sab.grow(16)` | `RangeError`, shrinking is not allowed |
| `sab.grow(128)` | `RangeError`, larger than `maxByteLength` |
Length-Tracking Views
When a typed array is created on a growable buffer without an explicit length, it is length-tracking. Its length follows the buffer as the buffer grows. A view created with an explicit length stays at that length.
The Node.js program below shows both kinds of view. The worker grows the buffer and then writes into the new space through a view it created before the grow.
const { Worker, isMainThread, workerData, parentPort } = require('node:worker_threads');
if (isMainThread) {
const sab = new SharedArrayBuffer(8, { maxByteLength: 64 });
const tracking = new Int32Array(sab); // follows the buffer
const fixed = new Int32Array(sab, 0, 2); // always 2 elements
const worker = new Worker(__filename, { workerData: sab });
worker.on('message', () => {
console.log(sab.byteLength); // 32
console.log(tracking.length); // 8
console.log(fixed.length); // 2
console.log(Atomics.load(tracking, 7)); // 99
});
} else {
const view = new Int32Array(workerData);
workerData.grow(32);
Atomics.store(view, 7, 99);
parentPort.postMessage('grown');
}The buffer and the main thread's views were never replaced. The main thread sees the new size because a grow on a shared buffer is visible to every agent that holds it.
Growing From Several Threads
grow is safe to call from more than one thread at once. Each call either takes effect or throws, and the buffer never ends up in a half-grown state. A thread can still read byteLength, decide to grow, and find that another thread has already grown the buffer past its target. Because growing to the current size or smaller-than-requested sizes can throw or do nothing, a common pattern is to compute the size you need and grow only when sab.byteLength is still below it. You should still be ready to catch a RangeError.
Exercise
A buffer is created with new SharedArrayBuffer(16, { maxByteLength: 32 }). You create a = new Int32Array(sab) and b = new Int32Array(sab, 0, 4), then call sab.grow(32) followed by sab.grow(24). What are a.length and b.length, and what happens on the second grow?
Show answer
After grow(32), a.length is 8 because it tracks the buffer (32 bytes / 4), and b.length stays 4 because its length was fixed. grow(24) throws a RangeError, because a growable SharedArrayBuffer cannot shrink.