Spec-Zone.ru › JavaScript

ArrayBuffer.prototype.transferToFixedLength()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It's been available across browsers since March 2024.

The transferToFixedLength() method of ArrayBuffer instances creates a new non-resizable ArrayBuffer with the same byte content as this buffer, then detaches this buffer.

Syntax

transferToFixedLength()
transferToFixedLength(newByteLength)

Parameters

newByteLength
The byteLength of the new ArrayBuffer. Defaults to the byteLength of this ArrayBuffer.
  • If newByteLength is smaller than the byteLength of this ArrayBuffer, the "overflowing" bytes are dropped.
  • If newByteLength is larger than the byteLength of this ArrayBuffer, the extra bytes are filled with zeros.

Return value

A new ArrayBuffer object. Its contents are initialized to the contents of this ArrayBuffer, and extra bytes, if any, are filled with zeros. The new ArrayBuffer is always non-resizable. The original ArrayBuffer is detached.

Exceptions

TypeError
Thrown if this ArrayBuffer is already detached, or if it can only be detached by designated operations. Currently, only certain web APIs are capable of creating ArrayBuffer objects with designated detaching methods, such as GPUBuffer.getMappedRange() and WebAssembly.Memory.buffer.

Description

Unlike transfer(), transferToFixedLength() always creates a non-resizable ArrayBuffer. This means newByteLength can be larger than the maxByteLength, even if this ArrayBuffer is resizable. See transferring ArrayBuffers for more information.

Examples

Transferring a resizable ArrayBuffer to fixed-length

const buffer = new ArrayBuffer(8, { maxByteLength: 16 });
const view = new Uint8Array(buffer);
view[1] = 2;
view[7] = 4;

const buffer2 = buffer.transferToFixedLength();
console.log(buffer2.byteLength); // 8
console.log(buffer2.resizable); // false
const view2 = new Uint8Array(buffer2);
console.log(view2[1]); // 2
console.log(view2[7]); // 4

Using transferToFixedLength, newByteLength can be larger than the maxByteLength of the original ArrayBuffer.

const buffer = new ArrayBuffer(8, { maxByteLength: 16 });
const view = new Uint8Array(buffer);
view[1] = 2;
view[7] = 4;

const buffer2 = buffer.transferToFixedLength(20);
console.log(buffer2.byteLength); // 20
console.log(buffer2.resizable); // false
const view2 = new Uint8Array(buffer2);
console.log(view2[1]); // 2
console.log(view2[7]); // 4

Specifications

Specification
ECMAScript® 2027 Language Specification
# sec-arraybuffer.prototype.transfertofixedlength

Browser compatibility

Desktop Mobile Server
Chrome Edge Firefox Opera Safari Chrome Android Firefox for Android Opera Android Safari on iOS Samsung Internet WebView Android WebView on iOS Bun Deno Node.js
transferToFixedLength
114
114
122
100
17.4
114
122
76
17.4
23.0
114
17.4
1.0.19
1.33
21.0.0

See also

  • Polyfill of ArrayBuffer.prototype.transferToFixedLength in core-js
  • es-shims polyfill of ArrayBuffer.prototype.transferToFixedLength
  • ArrayBuffer
  • ArrayBuffer.prototype.detached
  • ArrayBuffer.prototype.transfer()

© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/transferToFixedLength

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API