Skip to main content

Built and signed on GitHub Actions

A Hyper-Performant GC-Minimizing UInt32Array-Backed Boolean Array in Typescript

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
2 months ago (0.6.0)
class BooleanArray
extends Uint32Array

A fast boolean array backed by a Uint32Array

Constructors

new
BooleanArray(size: number)

Creates a new BooleanArray

Properties

readonly
size: number

Methods

clear(): this

Clear the array

Creates a copy of this BooleanArray

Get the boolean state of a bit

getBools(
startIndex: number,
count: number,
): boolean[]

Add bulk operations for better performance

Get the index of the first set bit starting from a given index

Get the index of the last set bit

Get the number of set bits in the array

Check if the array is empty

setAll(): this

Set all bits to true

setBool(
index: number,
value: boolean,
): this

Set the boolean state of a bit

setRange(
startIndex: number,
count: number,
value: boolean,
): this

Set a range of bits to a given value

Toggle the boolean state of a bit

truthyIndices(
startIndex?: number,
endIndex?: number,
): IterableIterator<number>

Get the indices of the set bits in the array

Static Properties

readonly
ALL_BITS: 4294967295

The mask for all bits (~0 >>> 0)

readonly
BITS_PER_INT: 32

The number of bits per integer

readonly
CHUNK_MASK: 31

The mask for the chunk offset

readonly
CHUNK_SHIFT: 5

The shift for the chunk offset

The threshold for what is considered a dense array

The threshold for what is considered a large array

readonly
MAX_SAFE_SIZE: 536870911

The maximum safe size for bit operations

Static Methods

Performs a bitwise AND operation with two BooleanArrays

Performs a bitwise difference operation with two BooleanArrays

Checks if two BooleanArrays are equal

Create a BooleanArray from an array of numbers, each number representing a bit to set to true.

fromObjects<T>(
size: number,
key: keyof T,
objs: T[],
): BooleanArray

Create a BooleanArray from an object, using the object's keys as the bit indices.

Get the chunk index for a given bool index

Get the number of chunks required to accommodate a given number of bools

Get the offset of a bool within a chunk

Performs a bitwise NAND operation with two BooleanArrays

Performs a bitwise NOR operation with two BooleanArrays

Performs a bitwise NOT operation with a BooleanArray

Performs a bitwise operation with two BooleanArrays

Performs a bitwise OR operation with two BooleanArrays

validateValue(
value: number,
maxSize?: number,
): number

Validate a value

Performs a bitwise XNOR operation with two BooleanArrays

Performs a bitwise XOR operation with two BooleanArrays

Add Package

deno add jsr:@phughesmcr/booleanarray

Import symbol

import { BooleanArray } from "@phughesmcr/booleanarray";

---- OR ----

Import directly with a jsr specifier

import { BooleanArray } from "jsr:@phughesmcr/booleanarray";

Add Package

npx jsr add @phughesmcr/booleanarray

Import symbol

import { BooleanArray } from "@phughesmcr/booleanarray";

Add Package

yarn dlx jsr add @phughesmcr/booleanarray

Import symbol

import { BooleanArray } from "@phughesmcr/booleanarray";

Add Package

pnpm dlx jsr add @phughesmcr/booleanarray

Import symbol

import { BooleanArray } from "@phughesmcr/booleanarray";

Add Package

bunx jsr add @phughesmcr/booleanarray

Import symbol

import { BooleanArray } from "@phughesmcr/booleanarray";