Need private packages and team management tools?Check out npm Teams »


1.1.0 • Public • Published

Collections Deep Equal

Collections like JavaScript’s native Maps and Sets, but using value equality (util.isDeepStrictEqual()) instead of reference equality

Source Package Continuous Integration


Maps and Sets in JavaScript use a notion of equality in which an object is considered equal to itself, but not to another object with the exact same keys and values:

const object = { name: "Leandro", age: 29 };
const deepEqualObject = { name: "Leandro", age: 29 };
assert(object === object);
assert(object !== deepEqualObject);
assert.deepEqual(object, deepEqualObject);
const map = new Map();
map.set(object, "value");
assert(map.get(object) === "value");
assert(map.get(deepEqualObject) === undefined);
const set = new Set();

One the one hand, this is good, because object and deepEqualObject really aren’t the same object. We may, for example, modify object and deepEqualObject would no longer be deep equal to it:

object.age = 30;
assert.notDeepEqual(object, deepEqualObject);
assert(object.age === 30);
assert(deepEqualObject.age === 29);

On the other hand, this is annoying, because you may know that you’re never mutating objects like that, and yet JavaScript doesn’t give you a way to customize the notion of equality used by Maps and Sets.


Collections Deep Equal provides MapDeepEqual and SetDeepEqual, which have the same API as JavaScript’s native Maps and Sets, except that their notion of equality is util.isDeepStrictEqual():

import { MapDeepEqual, SetDeepEqual } from "collections-deep-equal";
const object = { name: "Leandro", age: 29 };
const deepEqualObject = { name: "Leandro", age: 29 };
const mapDeepEqual = new MapDeepEqual();
mapDeepEqual.set(object, "value");
assert(mapDeepEqual.get(object) === "value");
assert(mapDeepEqual.get(deepEqualObject) === "value");
const setDeepEqual = new SetDeepEqual();


Install with npm:

$ npm install collections-deep-equal

The package comes with type definitions for TypeScript.

If you wish to replace JavaScript’s Maps and Sets seamlessly, rename on import:

import {
  MapDeepEqual as Map,
  SetDeepEqual as Set
} from "collections-deep-equal";



Collections Deep Equal hasn’t been benchmarked, but it probably is orders of magnitude slower than the native collections, because for every access it iterates over all keys and calls deepEqual() on them. It’s a straightforward, if naive, implementation.


If you mutate objects, then the collections using them change as well:

const object = { name: "Leandro", age: 29 };
const deepEqualObject = { name: "Leandro", age: 29 };
const mapDeepEqual = new MapDeepEqual();
mapDeepEqual.set(object, "value");
object.age = 30;
deepEqualObject.age = 30;

Additional Features


  new MapDeepEqual([
    ["a", new SetDeepEqual([1])],
    ["b", new SetDeepEqual([2])]
    new MapDeepEqual([
      ["b", new SetDeepEqual([3])],
      ["c", new SetDeepEqual([4])]
  new MapDeepEqual([
    ["a", new SetDeepEqual([1])],
    ["b", new SetDeepEqual([2, 3])],
    ["c", new SetDeepEqual([4])]
  new SetDeepEqual([1]).merge(new SetDeepEqual([2])),
  new SetDeepEqual([1, 2])


assert(JSON.stringify(new MapDeepEqual([["a", 1]])) === `[["a",1]]`);
assert(JSON.stringify(new SetDeepEqual([1, 2])) === `[1,2]`);

Related Work

People Discussing the Issue

Other Libraries That Implementation Alternative Collections

The advantages of Collections Deep Equal over these libraries are:

  1. You don’t have to buy into completely new data structures like Immutable.js’s Records. These other data structures may have different APIs and therefore a bit of a learning curve; they may be more difficult to inspect in debuggers; they may not work well with other libraries, forcing you to convert back and forth; and they may annoying to use in TypeScript.

  2. The notion of equality is determined by the data structures, not by the elements. In most of these libraries, elements are forced to implement equals() and hash(), which makes sense in a object-oriented style, but not in a functional style.

  3. Immutability is possible and encouraged, but not enforced. For better and for worse.

  4. Collections Deep Equal is so simple that you could maintain it yourself if it’s abandoned, like some of the packages above seem to have been. But don’t worry, Collections Deep Equal is being used in my dissertation, so it’ll stick around.

Other Approaches to Immutability

These libraries don’t provide new data structures. They’re just facilitating the use of immutable data structures, which may pave the way to a new notion of equality.

Very Similar But Incomplete Approaches

These libraries are very similar to Collections Deep Equal in spirit, but their implementations are either incomplete, or they lack type definitions, and so forth.

Definitive Solutions

Proposals to change the JavaScript language in ways that would make this package obsolete.


npm i collections-deep-equal

DownloadsWeekly Downloads






Unpacked Size

30.4 kB

Total Files


Last publish


  • avatar