How can you grow an ArrayBuffer after it's been created?
Short answer
Historically ArrayBuffers were fixed-length once allocated, so "growing" one meant allocating a new, larger buffer and copying the old data in.
Advertisement
AModel answer
Historically ArrayBuffers were fixed-length once allocated, so "growing" one meant allocating a new, larger buffer and copying the old data in. Modern JS (ES2024) added ArrayBuffer.prototype.resize() for buffers created with a maxByteLength option, allowing in-place growth up to that cap.
const buf = new ArrayBuffer(8, { maxByteLength: 32 });
buf.resize(16);
console.log(buf.byteLength); // 16Real-world example
Growing a binary message buffer incrementally while streaming data from a socket.
Likely follow-ups
- What's the performance cost of resizing versus pre-allocating?
Related topics