JavaScript ArrayBuffer transfer() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayBuffer

Definition

buffer.transfer(newLength) moves the contents of an ArrayBuffer into a new ArrayBuffer and detaches the original. After the call the old buffer has byteLength 0, its detached property is true, and methods such as slice() throw on it. Engines can often hand the memory over without copying. With newLength you can grow the result (new bytes are zero) or shrink it in the same step. Added in ES2024.

Remember
const moved = buffer.transfer();

moved.byteLength   → same size, same bytes
buffer.byteLength  → 0
buffer.detached    → true
buffer.slice(0)    → TypeError (detached)

transfer(bigger)   → extra bytes are 0
transfer(smaller)  → bytes past the end are dropped
resizable source   → resizable result (same maxByteLength)

Syntax

Call transfer on the buffer you want to give away, optionally with a new length.

JavaScript
buffer.transfer()
buffer.transfer(newLength)
CallWhat it does
buffer.transfer()Move all bytes into a new buffer of the same length.
buffer.transfer(newLength)Move the bytes into a new buffer of newLength bytes, zero-filled or truncated.

Added in ES2024. A resizable buffer gives a resizable result with the same maxByteLength. Use transferToFixedLength() when you always want a fixed-length result.

Library declarations
JavaScript
buffer.transfer(newLength)   // returns a new ArrayBuffer

// newLength: default buffer.byteLength
// afterwards: buffer.detached === true, buffer.byteLength === 0
// if buffer is resizable, newLength must be <= buffer.maxByteLength

Parameters

ParameterDescription
newLengthOptional. Size in bytes of the new buffer (default: the current byteLength). Larger values add zero bytes; smaller values cut bytes off the end. For a resizable buffer it must not exceed maxByteLength.

Return Value

Return ValueDescription
ArrayBufferA new ArrayBuffer that now owns the data. The buffer you called it on is detached.

JavaScript Examples

Example 1 — Move the Bytes and Detach the Original

Hand a finished buffer to a new owner, such as a Web Worker or a library. After the transfer the old reference can no longer read or change the bytes.

JavaScript
const original = new Uint8Array([1, 2, 3, 4]).buffer;
const moved = original.transfer();

console.log(moved.byteLength);                                   // 4
console.log(JSON.stringify(Array.from(new Uint8Array(moved))));  // [1,2,3,4]
console.log(original.byteLength);                                // 0
console.log(original.detached);                                  // true
Try It Yourself

How It Works

  1. Move instead of copy. transfer() creates moved with the same 4 bytes; the engine may reuse the memory rather than copy it.
  2. The old handle is emptied. original now reports byteLength 0 and detached as true.
  3. One owner at a time. Only moved can reach the data, which stops two parts of a program from editing the same bytes by accident.

Example 2 — Grow or Shrink While Moving

Pass newLength to change the size during the transfer, and watch what happens to a view that was built on the old buffer.

JavaScript
const show = (buf) => JSON.stringify(Array.from(new Uint8Array(buf)));

const buffer = new Uint8Array([1, 2, 3, 4]).buffer;
const oldView = new Uint8Array(buffer);

const bigger = buffer.transfer(6);
console.log(show(bigger));     // [1,2,3,4,0,0]
console.log(oldView.length);   // 0

const smaller = bigger.transfer(2);
console.log(show(smaller));    // [1,2]
console.log(bigger.detached);  // true
Try It Yourself

How It Works

  1. Grow during the move. transfer(6) keeps the four bytes and appends two zero bytes in the new buffer.
  2. Old views go dark. oldView was built on the now-detached buffer, so its length drops to 0. Create fresh views on the new buffer instead.
  3. Shrink during the move. bigger.transfer(2) keeps only the first two bytes, and bigger becomes detached in turn.

Example 3 — Resizability Carries Over

A resizable source produces a resizable result. The example also shows the two errors you will meet most often.

JavaScript
const growable = new ArrayBuffer(4, { maxByteLength: 8 });
const next = growable.transfer();

console.log(next.resizable);      // true
console.log(next.maxByteLength);  // 8

next.resize(8);
console.log(next.byteLength);     // 8

try {
  growable.slice(0);
} catch (err) {
  console.log(err.name);          // TypeError
}

try {
  next.transfer(20);
} catch (err) {
  console.log(err.name);          // RangeError
}
console.log(next.detached);       // false
Try It Yourself

How It Works

  1. Same limit, still resizable. The source allowed up to 8 bytes, so next is resizable with the same maxByteLength and resize(8) works.
  2. Detached means unusable. Calling slice() on the moved-from growable throws a TypeError.
  3. Limits still apply. transfer(20) asks for more than 8 bytes and throws a RangeError; next stays attached because nothing moved.

Browser & Runtime Support

transfer(), transferToFixedLength(), and the detached property are part of ES2024.

✓ Newer · ES2024

ArrayBuffer.prototype.transfer()

In recent Chrome, Edge, Firefox, Safari, and Opera, and in current Node.js (22 LTS and newer; the examples were run on Node.js 24). Browsers shipped it at different times, so check a compatibility table such as MDN's for your targets.

Recent Recent browsers only
Google Chrome Recent versions
Supported
Mozilla Firefox Recent versions
Supported
Apple Safari Recent versions
Supported
Microsoft Edge Recent versions
Supported
Node.js Node.js 22 LTS+ · check with typeof
Supported
Deno Recent versions
Supported
Internet Explorer No support
Not supported
transfer() Recent browsers

Bottom line: Feature-detect with typeof ArrayBuffer.prototype.transfer === "function". Fallbacks: buffer.slice(0, newLength) copies (without detaching), and postMessage(buffer, [buffer]) still moves buffers between threads.

Frequently Asked Questions

It creates a new ArrayBuffer with the same bytes, optionally at a new length, and detaches the original. The old buffer becomes length 0 and can no longer be read, sliced, or resized.
A detached ArrayBuffer has given its memory away. Its byteLength is 0, its detached property is true, typed arrays over it have length 0, and methods such as slice(), resize(), and transfer() throw a TypeError.
worker.postMessage(buffer, [buffer]) moves a buffer to another thread instead of copying it, and the sender's buffer becomes detached. transfer() does the same ownership move inside one thread.
Usually. slice() always copies; transfer() lets the engine move the memory, which can avoid a copy, especially at the same length. The exact cost depends on the engine.
transfer() keeps resizability: a resizable buffer produces a resizable result with the same maxByteLength. transferToFixedLength() always produces a fixed-length buffer. Both detach the original.

Next: transferToFixedLength()

Move the bytes into a buffer whose size is locked for good.

transferToFixedLength() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful