function generateRandomBitmasks(count = 100) {
const bitmasks = new Array(count);
for (let i = 0; i < count; i++) {
let bitLength = Math.floor(Math.random() * (32 - 5 + 1)) + 5;
bitmasks[i] = Math.floor(Math.random() * (1 << bitLength));
}
return bitmasks;
}
var bitmasks = generateRandomBitmasks();
var packed = packBitmasks(bitmasks);
var packedV8 = packBitmasksV8(bitmasks);
function packBitmasks(bitmasks) {
let totalBits = bitmasks.length * 32;
let packedSize = Math.ceil(totalBits / 8);
let packed = new Uint8Array(packedSize);
let bitIndex = 0;
for (let i = 0; i < bitmasks.length; i++) {
let value = bitmasks[i];
for (let b = 0; b < 32; b++) {
if (bitIndex >= totalBits) break;
let bytePos = bitIndex >> 3;
let bitPos = bitIndex % 8;
if (value & (1 << b)) {
packed[bytePos] |= (1 << bitPos);
}
bitIndex++;
}
}
return packed;
}
function unpackBitmasks(packed, length) {
let bitmasks = new Uint32Array(length);
let bitIndex = 0;
for (let i = 0; i < length; i++) {
let value = 0;
for (let b = 0; b < 32; b++) {
if (bitIndex >= packed.length * 8) break;
let bytePos = bitIndex >> 3;
let bitPos = bitIndex % 8;
if (packed[bytePos] & (1 << bitPos)) {
value |= (1 << b);
}
bitIndex++;
}
bitmasks[i] = value;
}
return bitmasks;
}
function packBitmasksV8(bitmasks) {
let packed = new Uint8Array(bitmasks.length * 4);
let packedView = new Uint32Array(packed.buffer);
for (let i = 0, len = bitmasks.length; i < len; i++) {
packedView[i] = bitmasks[i];
}
return packed;
}
function unpackBitmasksV8(packed) {
let packedView = new Uint32Array(packed.buffer);
let bitmasks = new Array(packedView.length);
for (let i = 0, len = packedView.length; i < len; i++) {
bitmasks[i] = packedView[i];
}
return bitmasks;
}