Skip to content

Instantly share code, notes, and snippets.

@alexanderwallin
Last active February 21, 2017 13:12
Show Gist options
  • Save alexanderwallin/0bf6a2a6c88186ec68de5f42f88a9fa1 to your computer and use it in GitHub Desktop.
Save alexanderwallin/0bf6a2a6c88186ec68de5f42f88a9fa1 to your computer and use it in GitHub Desktop.
Does this have side effects?
export const readUint16 = (typedArr, offset) => {
const buf = new ArrayBuffer(2)
const uint8 = new Uint8Array(buf)
const uint16 = new Uint16Array(buf)
uint8[0] = typedArr[offset]
uint8[1] = typedArr[offset + 1]
return uint16[0]
}
const buf = new ArrayBuffer(16)
const uint8 = new Uint8Array(buf)
const uint16 = new Uint16Array(buf)
export const readUint16 = (typedArr, offset) => {
uint8[0] = typedArr[offset]
uint8[1] = typedArr[offset + 1]
return uint16[0]
}
const data = new Uint8Array(8)
data[0] = Math.pow(2, 0)
data[1] = Math.pow(2, 1)
data[2] = Math.pow(2, 2)
data[3] = Math.pow(2, 3)
data[4] = Math.pow(2, 4)
data[5] = Math.pow(2, 5)
data[6] = Math.pow(2, 6)
data[7] = Math.pow(2, 7)
console.time('letsgo')
for (let i = 0; i < 1000000; i++) {
readUInt8(data, 0)
readUInt16(data, 0)
readUInt32(data, 0)
readUInt8(data, 7)
readUInt16(data, 6)
readUInt32(data, 4)
readFloat32(data, 0)
readFloat32(data, 4)
readFloat64(data, 0)
}
console.timeEnd('letsgo')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment