input is either the number of addressable bits (rounded up internally to
a whole number of storage items) or an existing typed array to use as
backing storage directly - the array is not copied, so external mutation
of it, or reuse of the same array across multiple BitArrays, is shared
state.
Choosing Uint8Array/Uint16Array/Uint32Array storage is a
memory-density trade-off only, not a CPU one - JS bitwise operators
coerce all operands to Int32 regardless of the typed array's element
width. Total bytes used is the same either way (1 bit always costs 1/8
byte); what changes is how many storage items that's split across.
Uint32Array (the default for a numeric size) uses the fewest
elements, which means less per-element overhead. Uint8Array gives the
finest granularity, which matters when handing the storage off to
byte-oriented interop - serialization, Buffer/network transfer, etc.
Uint16Array is a middle ground, useful mainly to match an existing
16-bit-oriented format.
Returns the number of addressable bits in the array.
Returns the number of bits allocated in the underlying storage.
This is not the same as size, which is the number of addressable bits.
Returns the underlying typed array backing this BitArray, without
copying it - useful for byte-oriented interop (serialization,
Buffer/network transfer, etc.) that needs direct access to the bytes.
Mutating the returned array mutates this BitArray directly.
Returns a string tag for Object.prototype.toString.call().
Sets the bit at bitIndex to 1.
Clears the bit at bitIndex back to 0.
Toggles the bit at bitIndex.
Test if the bit at bitIndex is set (1 not 0).
Create an independent copy: a new BitArray with its own storage (of the
same concrete type) and the same size. Unlike passing a typed array
into the constructor, this does not alias the original storage.
Clears all bits back to 0, reusing the existing allocation.
Population count: total number of set bits across the whole array.
Iterates each bit in index order, yielding true for set bits and
false for unset bits.
Fixed-size bit array backed by an unsigned typed array, used as dense storage for structures like Bloom filters where individual bits need to be set/tested without the overhead of a boolean array (8x smaller).