Skip to content
Eugene Lazutkin edited this page Jun 26, 2026 · 1 revision

union

Sorted merge of two or more streams with cross-stream deduplication — each distinct value (by the comparator) is emitted once. {1,2,3} ∪ {2,3,4} = {1,2,3,4}. Distinct from merge, which preserves duplicates.

import union from 'stream-sorting/sorted/union.js';

for await (const v of union([streamA, streamB])) {
  // each distinct value once, in sorted order
}

streams is an array of sorted AsyncIterable<T> | Iterable<T>; the result is an AsyncIterable<T>. Duplicates are removed both across streams and within a single stream. Built on merge plus an adjacent-dedup pass.

Options

Option Type Default Description
compare (a, b) => number natural order Orders items and defines equality. compare OR lessFn.
lessFn (a, b) => boolean Strict-less comparator. compare OR lessFn.

Two items are "equal" (and so deduplicated) when the comparator ranks neither below the other. An out-of-order input throws at runtime.

See also

merge, intersection, difference.

Clone this wiki locally