Skip to content

structOfArraysDisjointSet<T>(elements?): GrowableDisjointSet<T>

Defined in: packages/disjoint-set/src/struct-of-arrays-disjoint-set.ts:57

Create a DisjointSet backed by a struct of arrays. Parent pointers live in an Int32Array and ranks in a Uint8Array, instead of one { parent, rank } object per element.

The algorithm is the one objectDisjointSet uses, union by rank with path halving, and the observable behavior is identical. Only the memory layout differs, with two consequences at scale:

  • Node memory. A { parent, rank } object plus its slot in the backing array measured 48 bytes per element in a prior V8 run (arm64), once the object header, the hidden-class pointer, and alignment are counted. The typed arrays cost exactly 5 bytes per element, so node storage shrinks by about 9x. Both generic layouts additionally keep the same element registry. Use indexedDisjointSet to remove that registry for dense integer IDs.
  • Locality. Parent pointers sit in one contiguous buffer, so the pointer chase inside find walks cache lines instead of scattered heap objects, and each read compiles to an indexed load rather than a property lookup.

A Uint8Array is wide enough for ranks. Union by rank keeps rank bounded by the height of the tree, which is at most log2(n), so 255 covers every element count an Int32Array can index.

The typed arrays double in capacity when they fill up, which makes add amortized O(1) and leaves at most 2x the needed capacity allocated.

T

Iterable<T, any, any>

Elements to insert, each in its own singleton set.

GrowableDisjointSet<T>

When elements yields the same element twice.