Skip to content

Instantly share code, notes, and snippets.

@jt0dd
Created October 10, 2020 13:18
Show Gist options
  • Select an option

  • Save jt0dd/b9c57298dab7af97bc3fd3d51e06e3f0 to your computer and use it in GitHub Desktop.

Select an option

Save jt0dd/b9c57298dab7af97bc3fd3d51e06e3f0 to your computer and use it in GitHub Desktop.
const readBit = (buffer, i, bit) => {
return (buffer[i] >> bit) % 2
}
const setBit = (i, bit, value) => {
if (value == 0) {
b8[i] &= ~(1 << bit)
} else {
b8[i] |= (1 << bit)
}
return b8
}
const readStr = (buffer) => {
return buffer.toString("utf8");
}
const setStr = (str) => {
return Buffer.from(str, 'utf8');
}
const readInt16 = (b16) => {
return b16.readUInt16BE(0); //parseInt(b.toString('hex'), 16)
}
const setInt16 = (int16) => {
b16.writeUInt16BE(int16)
return b16;
}
const readInt32 = (b32) => {
return b32.readUInt32BE(0);
}
const setInt32 = (int32) => {
b32.writeUInt32BE(int32, 0); // 00 00 01 53 00 00 00 00
return b32;
}
const readInt64 = (b64) => {
//console.log("b64.readBigUInt64BE(0)", b64.readBigUInt64BE(0))
return Number(b64.readBigUInt64BE(0));
}
const setInt64 = (int64) => {
b64.writeBigUInt64BE(BigInt(int64), 0)
return b64;
}
const readFloat64 = (b64) => {
//console.log("read float64", b64, b64.readDoubleBE(0))
return b64.readDoubleBE(0);
}
const setFloat64 = (float64) => {
b64.fill(0);
b64.writeDoubleBE(float64, 0);
//console.log("write float64", float64, b64)
return b64;
}
const readFloat32 = (b32) => {
//console.log("reading b32", b32)
return b32.readUInt32BE(0) / 100
}
const setFloat32 = (float32) => {
b32.writeUInt32BE(float32 * 100, 0)
return b32;
}
const readBuffer = (buffer) => {
readCounter++
readStart = performance.now()
index = 0;
obj = {};
for (i = 0; i < bp.length; i++) {
type = bp[i];
len = types[type];
buff = buffer.slice(index, index + len);
index += len;
key = keys[i];
map = maps[i];
if (type === 0) {
timerStart = performance.now()
data = readBit(buff, 0, 0);
duration0 += performance.now() - timerStart
c0++
} else if (type === 1) {
timerStart = performance.now()
data = readStr(buff);
duration1 += performance.now() - timerStart
c1++
} else if (type === 2) {
timerStart = performance.now()
data = readInt16(buff);
if (map) data = map(data);
duration2 += performance.now() - timerStart
c2++
} else if (type === 3) {
timerStart = performance.now()
data = readInt32(buff);
duration3 += performance.now() - timerStart
c3++
} else if (type === 4) {
timerStart = performance.now()
data = readInt64(buff);
duration4 += performance.now() - timerStart
c4++
} else if (type === 5) {
timerStart = performance.now()
data = readFloat64(buff);
duration5 += performance.now() - timerStart
c5++
} else if (type === 6) {
timerStart = performance.now()
data = readFloat32(buff);
duration6 += performance.now() - timerStart
c6++
}
if (map) {
timerStart = performance.now()
data = map(data);
duration7 += performance.now() - timerStart
c7++
}
obj[key] = data;
}
duration9 += performance.now() - readStart
if (readCounter % 100000 === 0) {
console.clear()
console.log("total time spent reading:", duration9)
console.log("duration0:", duration0, "[", (((duration0 / c0) / (duration9 / readCounter)) * 100).toFixed(2) + "%] readBit") // duration0: 30.224334597587585 [ 1.32%] readBit
console.log("duration1:", duration1, "[", (((duration1 / c1) / (duration9 / readCounter)) * 100).toFixed(2) + "%] readStr") // duration1: 183.31049799919128 [ 4.00%] readStr
console.log("duration2:", duration2, "[", (((duration2 / c2) / (duration9 / readCounter)) * 100).toFixed(2) + "%] readInt16") // not testing any of these: NaN
console.log("duration3:", duration3, "[", (((duration3 / c3) / (duration9 / readCounter)) * 100).toFixed(2) + "%] readInt32") // duration3: 93.2903823852539 [ 1.36%] readInt32
console.log("duration4:", duration4, "[", (((duration4 / c4) / (duration9 / readCounter)) * 100).toFixed(2) + "%] readInt64") // duration4: 176.68701934814453 [ 3.86%] readInt64
console.log("duration5:", duration5, "[", (((duration5 / c5) / (duration9 / readCounter)) * 100).toFixed(2) + "%] readFloat64") // not testing any of these: NaN
console.log("duration6:", duration6, "[", (((duration6 / c6) / (duration9 / readCounter)) * 100).toFixed(2) + "%] readFloat32") duration6: 298.7579890489578 [ 1.45%] readFloat32
console.log("duration7:", duration7, "[", (((duration7 / c7) / (duration9 / readCounter)) * 100).toFixed(2) + "%] map") // duration7: 344.6284147500992 [ 2.51%] map
console.log("converted buffer", buffer, "to obj", obj)
}
//
return obj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment