by vanderZwan · 4y ago · 0 comments
Nowadays we have TextDecoder and BigInt64Array so I'm pretty sure we actually could do it[1][2].
I doubt it would be faster than calling <number>.toString(2), but in principle we could port this.
[0] https://news.ycombinator.com/item?id=10517332
[1] https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder
[2] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
const toBinary = (() => {
const u64 = new BigUint64Array(1);
const u8 = new Uint8Array(u64.buffer)
u8[0] = 255;
const decoder = new TextDecoder();
return (number) => {
u64[0] = 0n;
u8[0] = number; // We're assuming little-endian architectures
u64[0] = (((u64[0] * 0x8040201008040201n) >> 7n) & 0x0101010101010101n) + 0x3030303030303030n
return decoder.decode(u8)
};
})()https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...