Note
This is one of 199 standalone projects, maintained as part of the @thi.ng/umbrella monorepo and anti-framework.
🚀 Please help me to work full-time on these projects by sponsoring me on GitHub. Thank you! ❤️
TypedArray-based sparse set implementations with extended ES Set API.
This package contains functionality which was previously part of and has been extracted from the @thi.ng/associative package.
Sparse sets provide super fast
(approx. 4x faster than the native Set
impl) insertion & lookups for
numeric values in the interval [0..n)
. The implementation in this
package provides most of the ES6 Set API and internally relies on 2 uint
typed arrays, with the actual backing type dependent on n
.
Furthermore, unless (or until) values are being removed from the set, they retain their original insertion order. For some use cases (e.g. deduplication of values), this property can be very useful.
import { defSparseSet } from "@thi.ng/sparse-set";
// create sparse set for value range 0 - 99 (uint8 backed)
const a = defSparseSet(100);
a.into([99, 42, 66, 23, 66, 42]);
// SparseSet8 { 99, 42, 66, 23 }
a.has(66)
// true
// sparse sets are iterable
[...a]
// [ 99, 42, 66, 23 ]
// attempting to add out-of-range values will fail
a.add(100)
// SparseSet8 { 99, 42, 66, 23 }
// create sparse set for 16 bit value range 0 - 0xffff (uint16 backed)
const b = defSparseSet(0x10000);
// SparseSet16 {}
STABLE - used in production
Search or submit any issues for this package
- @thi.ng/associative - ES Map/Set-compatible implementations with customizable equality semantics & supporting operations
yarn add @thi.ng/sparse-set
ESM import:
import * as ss from "@thi.ng/sparse-set";
Browser ESM import:
<script type="module" src="https://esm.run/@thi.ng/sparse-set"></script>
For Node.js REPL:
const ss = await import("@thi.ng/sparse-set");
Package sizes (brotli'd, pre-treeshake): ESM: 988 bytes
Note: @thi.ng/api is in most cases a type-only import (not used at runtime)
TODO
If this project contributes to an academic publication, please cite it as:
@misc{thing-sparse-set,
title = "@thi.ng/sparse-set",
author = "Karsten Schmidt",
note = "https://thi.ng/sparse-set",
year = 2019
}
© 2019 - 2024 Karsten Schmidt // Apache License 2.0