uniqs: Unique and Union Utility
uniqs is a lightweight JavaScript utility designed to create unique lists and unions of multiple lists, defining uniqueness based on strict object equality. Currently at version 2.0.0, this package distinguishes itself by prioritizing simplicity and a minimal footprint over raw performance, making it a suitable alternative to more comprehensive libraries like Lodash or Underscore for basic deduplication tasks. Its primary purpose is to provide a straightforward function for de-duplicating items and combining arrays without complex features or external dependencies, making it ideal for scenarios where bundle size and directness are critical. Items are maintained in their first-appearance order, reflecting their initial encounter in the input lists.
Common errors
-
Expected [ { a: 1 }, { a: 1 } ] to be unique, but got two distinct objects.cause `uniqs` uses strict (===) equality, so two different object instances, even with identical content, are considered unique.fixIf you need deep equality, serialize objects (e.g., `JSON.stringify`) or use a custom comparison function before passing them to `uniqs`, or switch to a library that supports deep equality. -
Application hangs or runs very slowly when processing a large array with `uniqs`.
cause The internal implementation of `uniqs` has O(n^2) complexity, making it inefficient for very large input arrays due to repeated `indexOf` calls.fixFor performance-critical applications with large datasets, use JavaScript's built-in `Set` for O(n) performance with primitive values, or a more optimized library function like Lodash's `uniq` for objects.
Warnings
- gotcha uniqs uses a simple `indexOf`-based deduplication, resulting in O(n^2) time complexity for the combined list. This can lead to significant performance degradation on large arrays (thousands of items or more).
- gotcha Uniqueness is determined solely by strict (`===`) object equality. This means two objects with identical properties but different references (e.g., `{a: 1}` and another `{a: 1}`) will be considered distinct items.
- breaking The transition to version 2.0.0 implies potential breaking changes as per semantic versioning. While specific details are not provided in the excerpt, developers upgrading from 1.x should expect behavioral shifts.
Install
-
npm install uniqs -
yarn add uniqs -
pnpm add uniqs
Imports
- uniqs
import { uniqs } from 'uniqs';const uniqs = require('uniqs'); - uniqs
const { uniqs } = require('uniqs');import uniqs from 'uniqs';
Quickstart
const uniqs = require('uniqs');
// Example usage to deduplicate a list
const foo = { id: 23 };
const list = [3, 2, 2, 1, foo, foo];
const uniqueList = uniqs(list);
console.log('Unique list:', uniqueList);
// Expected: [3, 2, 1, { id: 23 }]
// Example usage to create a union of multiple lists
const listA = [2, 1, 1];
const listB = [2, 3, 3, 4];
const listC = [4, 3, 2];
const unionList = uniqs(listA, listB, listC);
console.log('Union list:', unionList);
// Expected: [2, 1, 3, 4]
// Example with individual items and a list
const mixedInput = uniqs(3, 2, 2, [1, 1, 2]);
console.log('Mixed input:', mixedInput);
// Expected: [3, 2, 1]