Speed-optimized BitSet implementation for modern browsers and JavaScript engines, uses typed arrays.
A BitSet (also called Bitmap or bit vector) is an ideal data structure to implement a
set when values being stored are reasonably small integers. It can be orders of magnitude
faster than a generic set implementation. In particular, a BitSet has fast support for set
operations (union, difference, intersection).
The TypedFastBitSet.js implementation optimizes for speed, leveraging commonly available features
like typed arrays. It can be several times faster than competitive alternatives. It is also entirely
dynamic, and has functions to minimize the memory usage. It should be supported by most of the modern
browsers and JavaScript engines. It is ideal for maintaining sets of integers when performance matters.
License: Apache License 2.0
Usage
const b = new TypedFastBitSet(); // initially emptyb.add(1); // add the value "1"b.has(1); // check that the value is present! (will return true)b.add(2);console.log("" + b); // should display {1,2}b.add(10);b.addRange(11, 13);b.array(); // would return [1, 2, 10, 11, 12, 13]let c = new TypedFastBitSet([1, 2, 3, 10]); // create bitset initialized with values 1,2,3,10
If you are using Node.js, you need to import the module:
const { TypedFastBitSet } = require("typedfastbitset");const b = new TypedFastBitSet(); // initially emptyb.add(1); // add the value "1"
Performance tip: in-place functions such as intersection, union and difference can be
much faster than functions generating a new bitmap (new_intersection, new_union
and new_difference) because they avoid creating a new object, a potentially
expensive process in JavaScript. For faster code, use as few TypedFastBitSet objects as
you can.
There is also SparseTypedFastBitSet which might perform better depending on the shape of your data.
This implementation starts as an array of values and switches to a bitset structure once enough entries have been added.
API
Constructor and Static Methods
new TypedFastBitSet(iterable?) — create a bitset, optionally initialized from an iterable of integers
TypedFastBitSet.fromWords(words: Uint32Array) — create a bitset from a raw word array
Basic Operations
add(index) — set bit at index to true
checkedAdd(index) — add value, returns 1 if added, 0 if already present
remove(index) — set bit at index to false
flip(index) — toggle bit at index
has(index) — returns true if bit at index is set
clear() — remove all values, reset memory
Range Operations
addRange(start, end) — set bits from start (inclusive) to end (exclusive)
removeRange(start, end) — clear bits from start (inclusive) to end (exclusive)
hasAnyInRange(start, end) — returns true if any bit in range is set
Iteration
array() — return an array of set bit indices
forEach(fn) — call fn for each set bit index
[Symbol.iterator]() — iterate over set bit indices (supports for...of)
Set Operations (in-place, modifies this)
union(other) — this = this | other
intersection(other) — this = this & other
difference(other) — this = this & ~other
difference2(other) — other = this & ~other (modifies other)
change(other) — this = this ^ other
Set Operations (new bitmap)
new_union(other) — returns new bitmap this | other
new_intersection(other) — returns new bitmap this & other
new_difference(other) — returns new bitmap this & ~other
new_change(other) — returns new bitmap this ^ other
Size Queries
size() — number of set bits
isEmpty() — returns true if no bit is set
union_size(other) — size of union without creating it
intersection_size(other) — size of intersection without creating it
difference_size(other) — size of difference without creating it
change_size(other) — size of symmetric difference without creating it
Comparison and Utility
equals(other) — returns true if both bitsets contain the same values
intersects(other) — returns true if bitsets share any value
clone() — returns a deep copy
trim() — reduce memory usage by removing trailing empty words
resize(index) — ensure capacity for the given index (may over-allocate for speed)
resizeTo(size) — ensure capacity for exactly size bits (no over-allocation)
toString() — string representation like {1,2,3}
npm install
npm install typedfastbitset
Testing
npm test
Is it faster?
TypedFastBitSet can be quite fast compared to competitive alternatives :
$ node benchmark/test.jsBenchmarking against:TypedFastBitSet.js: https://github.com/lemire/TypedFastBitSet.jsroaring: https://www.npmjs.com/package/roaringinfusion.BitSet.js from https://github.com/infusion/BitSet.js 5.0.0tdegrunt.BitSet from https://github.com/tdegrunt/bitsetmattkrick.fast-bitset from https://github.com/mattkrick/fast-bitset 1.3.2standard Set object from JavaScriptNot all libraries support all operations. We benchmark what is available.Platform: darwin 25.2.0 arm64Apple M4 MaxNode version 25.6.1, v8 version 14.1.146.11-node.19
To reproduce, install benchmark dependencies and run:
npm install benchmark fastbitset bitset bitset.js fast-bitset roaring minimistnpm run build:packagenpm run benchmark