@sepiariver/unique-set
v3.3.0
Published
A Set-like collection that deduplicates by deep value equality using a streaming structural hash and fast-equals.
Maintainers
Readme
@sepiariver/unique-set
A Set that deduplicates by deep value equality.
- In the reported benchmark run against six deep-equality libraries,
UniqueSetwas the fastest on 12 of 13 deduplication workloads and on lookup (up to 1500x faster in specific workloads, see margins in PERF.md - Findings). On most workloads the closest competitor was hash-it +Map, which doesn't confirm hash matches (PERF.md - Collision safety).UniqueSetwas also the only one to return the expected count on every workload, where the expected counts followfast-equalssemantics (PERF.md - Semantic differences). Some test datasets were imported from those competitors, with attribution. UniqueSetcompares built-ins by content: URL, Error, typed arrays, ArrayBuffer, DataView, boxed primitives, and React/Preact elements (equality semantics).UniqueSetis collision-safe: a streaming, non-cryptographic 32-bit FNV-1a hash points to a bucket, andfast-equalsconfirms every match, so distinct values that share a hash are distinguished (PERF.md - Collision safety).add(),has()anddelete()take expected constant time in the number of stored values, given a well-distributed hash. Each call also hashes its argument, in time proportional to the argument's size, and compares it with any stored values that share its hash.
Installation
npm install @sepiariver/unique-setSupports ESM and CommonJS. UniqueSet is an alias of MapSet, and the two are the same class.
import { MapSet, UniqueSet } from "@sepiariver/unique-set";
const { MapSet, UniqueSet } = require("@sepiariver/unique-set");Version 3 has breaking changes: older versions are deprecated, and their configuration options are gone. Usage matches the native Set, apart from the differences listed below.
API
Constructor
new MapSet(iterable?)
new UniqueSet(iterable?)Accepts any iterable (array, Set, generator, etc.) and discards duplicate values.
Methods
| Method | Description |
| --- | --- |
| add(value) | Adds value if no deeply-equal value exists. Returns this. |
| has(value) | Returns true if a deeply-equal value is in the set. |
| delete(value) | Removes the deeply-equal value, whatever its reference. Returns true if found. |
| clear() | Removes all values. |
| forEach(cb, thisArg?) | Calls cb(value, value, set) for each value. |
| values() | Returns an iterator over all values. |
| [Symbol.iterator]() | Makes the set iterable (e.g., for...of, spread). |
| size | The number of unique values in the set. |
| keys() | The same as values(), as on Set. |
| entries() | Returns an iterator of [value, value] pairs, as on Set. |
| union(other) | Returns a new set with the values of this set, then those of other that it lacks. other is any set-like object (size, has(), keys()), as for the ES2025 Set methods. |
| intersection(other) | Returns a new set with the values of this set that other also has. |
| difference(other) | Returns a new set with the values of this set that other does not have. |
| symmetricDifference(other) | Returns a new set with the values in exactly one of this set and other. |
| isSubsetOf(other) | Returns true if other has every value of this set. |
| isSupersetOf(other) | Returns true if this set has every value of other. |
| isDisjointFrom(other) | Returns true if this set and other have no value in common. |
Examples
const set = new UniqueSet();
set.add({ a: 1, b: 2 });
set.add({ b: 2, a: 1 }); // same value, different key order: not added
set.size; // 1
set.has({ a: 1, b: 2 }); // true (deep equality, not reference)
set.add([1, [2, 3]]);
set.add([1, [2, 3]]); // duplicate nested array: not added
set.size; // 2
set.delete({ a: 1, b: 2 }); // true
set.size; // 1
new UniqueSet(["a", "a", 1, 1, [1, 2], [1, 2]]).size; // 3, where Set gives 4Considerations
- Memory: Each value is stored once, as the value of its own entry in a
Mapkeyed by its 32-bit structural hash. Only values that share a hash need more: those values get a small wrapper object and an entry keyed by that wrapper, which keeps them in insertion order. - Collisions: At 100,000 values, about one pair of values is expected to share a hash (the birthday bound for 32 bits).
fast-equalsdistinguishes them, so a collision costs a comparison but never a wrong result. - Equality semantics: The structural hash follows
fast-equals' comparison for every kind of value, so equal values share a hash, with the one exception under look-alikes below.- Plain objects: Key order is ignored.
- Arrays: Element order matters.
Setvalues: Insertion order is ignored, and elements compare by deep equality:new Set([1, 2])equalsnew Set([2, 1]).Mapvalues: Insertion order is ignored, and both keys and values compare by deep equality.- Primitives:
NaNequalsNaN, and0equals-0. - Class instances: Equal only to instances of the same class, and compared by their own enumerable properties.
#privatefields are invisible to the comparison, so instances that differ only in private state are treated as equal and only the first is kept:new Counter(1)andnew Counter(2)deduplicate to one value whenCounterstores its count in#count. When that state should count, expose it as an enumerable property or add a derived plain value. - Other built-ins:
Date,RegExp,URL,ArrayBuffer, typed arrays,DataViewand boxed primitives compare by contents. Enumerable properties added to any of these, or to aMaporSet, are ignored. Errors compare theirname,message,stack,causeand enumerable own properties. ADataViewwhose buffer has been detached, or has shrunk below it, compares as an empty view at offset 0. React and Preact elements ignore their owner fields (_owner,__o,__v). - Objects compared by reference: Built-ins without a comparison of their own, such as
URLSearchParams,Blob,WeakMapandPromise, and class instances with athenmethod, are equal only to themselves. Each is hashed by its identity, so it gets its own hash, andstructuralHash()returns a value that identifies that object only within the running process. - Look-alikes: An object that borrows a built-in's
Symbol.toStringTagwithout being that built-in, such as a class whose tag is"URL", hashes to one fixed value per tag, and comparison tells such objects apart. One case is not covered:fast-equalstreats two objects taggedURLas equal whenever theirhrefproperties match, so an object created fromURL.prototypewith anhrefof its own, or a realURLwhosehrefis shadowed by an own property, can be stored next to an equal realURL. Covering it would mean giving everyURLthe same hash. - Values from another realm (such as a
vmcontext or an iframe): Never equal to the same value built in this realm, because constructors must match, but compared with each other by contents like the built-ins above. - Functions and symbols: Compared by reference. They hash by their string representation (
String(value)), so same-source functions may share a hash.
- Circular references: Supported.
- A value nested more than 256 objects deep, which includes every circular value, is hashed from its top 3 levels only and compared with
fast-equals'circularDeepEqualsemantics. Such values that agree in their top 3 levels share a hash and are told apart by comparison, one by one. circularDeepEqualpairs each object with its counterpart, so two values are equal only when their cycles line up:a = { self: a }equals{ self: b }whereb = { self: b }, butx = [x]does not equal[[y]]wherey = [y].- UniqueSet uses its own copy of that pairing check, because the one in
fast-equals6.1.1 overflows the stack on some equalSets andMaps that contain themselves, when their insertion orders differ. - Comparing two equal acyclic values still overflows the stack once they are nested deep enough: somewhere past 1,000 levels in Node.js 24 with the default stack size, depending on the values' shape.
- A value nested more than 256 objects deep, which includes every circular value, is hashed from its top 3 levels only and compared with
Differences from native Set
A
MapSetis notinstanceof Set.Stored values must not change. A value is stored by reference under the hash it had when added, and is never rehashed. Changing anything its hash or equality depends on while it is stored, including state reachable through nested objects, makes
has()anddelete()miss it and letsadd()store the same reference a second time. Delete the value, change it, then add it again.Set algebra compares a native
Setargument's values by deep equality, where the ES2025 algorithm would call itshas(), which compares by reference. Sonew UniqueSet([{ a: 1 }]).intersection(new Set([{ a: 1 }]))has size 1, whichever side is smaller. This covers only aSetof this realm whose prototype isSet.prototypeand whosehas()andkeys()are the built-ins. A value theSetholds by reference costs no hashing. An object it doesn't hold that way makes the call hash every object in theSetonce, so the cost grows with theSet's size even when this set is small.Set algebra uses any other argument, including a
Setfrom another realm, as the ES2025 algorithm does, callingother.has()wherever the algorithm calls it, so membership followsother's equality when this set drives the loop. Whenother.has()compares by reference,intersection(),difference()andisDisjointFrom()then depend on which side is smaller, andisSubsetOf()matches by reference. These arguments are not handled specially, because the right behavior for them is ambiguous:- A
Map: its keys could be read as a set's values, matched by deep equality, or asMapkeys, matched by reference.new UniqueSet([{ a: 1 }]).intersection(new Map([[{ a: 1 }, true]]))has size 0, because the loop runs over this set andMap.hascompares by reference. With{ b: 2 }added to the UniqueSet, the same call has size 1, because the loop then runs over theMap's keys and looks each one up here by deep equality. - A subclass of
Set, or aSetwith its ownhas(): an overridinghas()may define the equality the caller wants, and bypassing it would discard that.
Pass a
MapSetasotherfor deep equality on every path.- A
Testing
- Clone this repo
npm installnpm run test
Benchmark commands are in PERF.md.
Issues
Issue reporting is encouraged: https://github.com/sepiariver/unique-set/issues
Contributing
Submit pull requests to https://github.com/sepiariver/unique-set/pulls
Contributors
Human
- @sepiariver
- @sakgoyal, who instigated and contributed ESM support.
AI
Through various versions of this package, I've leveraged Opus 4.x through 5.5 for research, analysis and contributions in the areas of:
- performance optimization
- testing and test fixture preparation
- competitive analysis and benchmarking
- native Set API parity
License
MIT
