Skip to content

Instantly share code, notes, and snippets.

@kevlened
Created January 4, 2018 17:29
Show Gist options
  • Save kevlened/6e6b69c857bb80073e02a52677672a3b to your computer and use it in GitHub Desktop.
Save kevlened/6e6b69c857bb80073e02a52677672a3b to your computer and use it in GitHub Desktop.
Fastest browser hex buffer operations
export function fromUint8Array(uint8Array) {
const view = new DataView(uint8Array.buffer)
const bl = view.byteLength, largeLength = bl - (bl % 4)
let hex = '', d = 0
for (; d < largeLength; d += 4) {
hex += ('00000000' + view.getUint32(d).toString(16)).slice(-8)
}
for (; d < bl; d++) {
let c = view.getUint8(d).toString(16)
hex += c.length < 2 ? '0' + c : c
}
return hex;
}
export function toUint8Array(str) {
if (str.length % 2) throw new Error(str + 'is not valid hex')
const sl = str.length, largeLength = sl - (sl % 8)
const uint8Array = new Uint8Array(sl / 2)
const view = new DataView(uint8Array.buffer)
let s = 0
for (; s < largeLength; s += 8) {
view.setUint32(s / 8, parseInt(str.substr(s, 8), 16))
}
for (; s < sl; s += 2) {
view.setUint8(s / 2, parseInt(str.substr(s, 2), 16))
}
return uint8Array
}
export function fromBuffer(buffer) {
return fromUint8Array(new Uint8Array(buffer))
}
export function toBuffer(str) {
return toUint8Array(str).buffer
}
export default {
fromUint8Array,
toUint8Array,
fromBuffer,
toBuffer
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment