Created
January 25, 2017 20:27
-
-
Save chrisj/3c7de7ca5fc9e26e29c766df0f66a866 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
[ | |
Uint8Array, | |
Uint8ClampedArray, | |
Uint16Array, | |
Uint32Array, | |
Int8Array, | |
Int16Array, | |
Int32Array, | |
Float32Array, | |
Float64Array | |
].forEach((type) => { | |
type.prototype.clear = function () { | |
// Float64Array is faster but since the array may not be a multiple of 8 bytes, we use Int8Array to set the remaining bytes | |
const fastView = new Float64Array(this.buffer, 0, (this.buffer.byteLength / Float64Array.BYTES_PER_ELEMENT)|0); | |
const byteView = new Int8Array(this.buffer, fastView.byteLength, this.buffer.byteLength % Float64Array.BYTES_PER_ELEMENT); | |
const length1 = fastView.length; | |
const length2 = byteView.length; | |
for (let i = 0; i < length1; i++) { | |
fastView[i] = 0; | |
} | |
for (let i = 0; i < length2; i++) { | |
byteView[i] = 0; | |
} | |
return this; | |
}; | |
}); | |
let arr = new Uint8Array(1024 * 1024 * 256); | |
for (let i = 0; i < 10; i++) { | |
let start = performance.now(); | |
arr.clear(); | |
let end = performance.now(); | |
console.log('time1', end - start); | |
let start2 = performance.now(); | |
arr.fill(0); | |
let end2 = performance.now(); | |
console.log('time2', end2 - start2); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment