This is an experimental technology, part of the ECMAScript 2016 (ES7) proposal.
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future version of browsers as the spec changes.
The static ArrayBuffer.transfer() method returns a new ArrayBuffer whose contents are 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
ArrayBufferobject from which to transfer from. - newByteLength
- The byte length of the new
ArrayBufferobject.
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
Not part of any current specification draft document, but has been proposed for ES7.
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | Not supported | Nightly build | Not supported | Not supported | Not supported |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | Not supported | Not supported | Nightly build | Not supported | Not supported | Not supported |