The static ArrayBuffer.transfer()
method returns a new ArrayBuffer
whose contents have been taken from the oldBuffer
's data and then is either truncated or zero-extended by newByteLength
. If newByteLength
is undefined
, the byteLength
of the oldBuffer
is used. This operation leaves oldBuffer
in a detached state.
Syntax
ArrayBuffer.transfer(oldBuffer [, newByteLength]);
Parameters
oldBuffer
- An {{jsxref("ArrayBuffer")}} object from which to transfer.
- newByteLength
- The byte length of the new
ArrayBuffer
object.
Description
The ArrayBuffer.transfer()
method allows you to grow and detach ArrayBuffer
objects. The ability to grow an ArrayBuffer
without copying has the advantage of being much faster for large buffers (similar to realloc). The ability to detach an ArrayBuffer
gives the developer explicit control over when the underlying memory is released. This avoids having to drop all references and wait for garbage collection.
Examples
var buf1 = new ArrayBuffer(40); new Int32Array(buf1)[0] = 42; var buf2 = ArrayBuffer.transfer(buf1, 80); buf1.byteLength; // 0 buf2.byteLength; // 80 new Int32Array(buf2)[0]; // 42 var buf3 = ArrayBuffer.transfer(buf2, 0); buf2.byteLength; // 0 buf3.byteLength; // 0
Specifications
{{WhyNoSpecStart}}Not part of any current specification draft document, but has been proposed for a future ECMA-262 edition.{{WhyNoSpecEnd}}
Browser compatibility
{{CompatibilityTable}}
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | {{CompatNo}} | {{CompatNightly("firefox")}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | {{CompatNo}} | {{CompatNo}} | {{CompatNightly("firefox")}} | {{CompatNo}} | {{CompatNo}} | {{CompatNo}} |