This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
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.
ArrayBuffer.transfer(oldBuffer [, newByteLength]);
oldBuffer
ArrayBuffer
object from which to transfer.ArrayBuffer
object.A new ArrayBuffer
object.
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.
var buf1 = new ArrayBuffer(40); new Int32Array(buf1)[0] = 42; var buf2 = ArrayBuffer.transfer(buf1, 80); buf1.byteLength; // 0 but if you use the polyfill then the value is still 40 buf2.byteLength; // 80 new Int32Array(buf2)[0]; // 42 var buf3 = ArrayBuffer.transfer(buf2, 0); buf2.byteLength; // 0 but if you use the polyfill then the value is still 80 buf3.byteLength; // 0
You can partially work around this by inserting the following code at the beginning of your scripts, allowing use of much of the functionality of transfer() in browsers that do not natively support it. This is not the exact equivalent of this API because browsers that natively support it are be able to internally use the C++ function realloc() which extends the length of the memory and only copies it to a new location as-needed as opposed to the following pollyfill which always copies the whole thing to a new space of memory, but this function transfers data from one ArrayBuffer to another ArrayBuffer.
if (!ArrayBuffer.transfer) { ArrayBuffer.transfer = function(source, length) { if (!(source instanceof ArrayBuffer)) throw new TypeError('Source must be an instance of ArrayBuffer'); if (length >= source.byteLength) return source.slice(0, length); var sourceView = new Uint8Array(source), destView = new Uint8Array(new ArrayBuffer(length)); destView.set(sourceView); return dest.buffer; }; }
Not part of any current specification draft document, but has been proposed for a future ECMA-262 edition.
Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|---|
Basic support | No | No | No | No | No | No |
Feature | Android webview | Chrome for Android | Edge mobile | Firefox for Android | IE mobile | Opera Android | iOS Safari |
---|---|---|---|---|---|---|---|
Basic support | No | No | No | No | No | No | No |
© 2005–2018 Mozilla Developer Network and individual 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/transfer