Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@domenic
Created July 26, 2018 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save domenic/b497fb15167b522758cbf372eaf0f081 to your computer and use it in GitHub Desktop.
Save domenic/b497fb15167b522758cbf372eaf0f081 to your computer and use it in GitHub Desktop.
ArrayBuffer.prototype.transfer not-really-a-polyfill
// This creates a copy, but does detach the source buffer
const mc = new MessageChannel();
ArrayBuffer.prototype.transfer = function () {
const result = this.slice();
mc.port1.postMessage(this, [this]);
return result;
};
const ab = new ArrayBuffer(10);
console.log(ab.byteLength);
const transferred = ab.transfer();
console.log(ab.byteLength);
console.log(transferred.byteLength);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment