Skip to content

Instantly share code, notes, and snippets.

@zryty
Forked from jussi-kalliokoski/dispose.js
Created October 15, 2020 12:44
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 zryty/f8f08b95ab52c4d20da1fbbdc769546c to your computer and use it in GitHub Desktop.
Save zryty/f8f08b95ab52c4d20da1fbbdc769546c to your computer and use it in GitHub Desktop.
A polyfill for ArrayBuffer#dispose
void function () {
if ( 'dispose' in ArrayBuffer.prototype ) return
var blob = new Blob([''], {
type: 'text/javascript'
})
var url = URL.createObjectURL(blob)
var worker = new Worker(url)
URL.revokeObjectURL(url)
Object.defineProperty(ArrayBuffer.prototype, 'dispose', {
writable: true,
enumerable: false,
value: function () {
worker.postMessage(this, [this])
}
})
}()
var x = new Float32Array(16)
console.log(x.length) // 16
x.buffer.dispose()
console.log(x.length) // 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment