# object-identity [](https://licenses.dev/npm/object-identity)
**A tiny, fast utility that canonicalizes any value into a stable identity, for structurally
equality.**
This is free to use software, but if you do like it, consider supporting me โค๏ธ
[](https://github.com/sponsors/maraisr)
[](https://www.buymeacoffee.com/marais)
## โก Features
- ๐งฌ **Canonical.** The same shape always produces the same id.
- ๐ **Deep and cycle-safe.** Handles nested objects, arrays, sets, maps, and circular references.
- ๐ **Fast.** See the [benchmarks](#-benchmark).
- ๐ชถ **Tiny.** Around 538B minified and gzipped, with zero
[dependencies](https://npm.anvaka.com/#/view/2d/object-identity/).
## โ๏ธ Install
- **npm** โ available as [`object-identity`](https://www.npmjs.com/package/object-identity)
- **JSR** โ available as [`@mr/object-identity`](https://jsr.io/@mr/object-identity)
## ๐ Usage
```ts
import { identify } from 'object-identity';
// same shape, different key order, same id
identify({ a: 1, b: 2 }) === identify({ b: 2, a: 1 }); // true
// a plain object and an equivalent Map produce the same id
identify({ a: 'b' }) === identify(new Map([['a', 'b']])); // true
// nesting, Sets, Dates, and RegExps are all supported
const key = identify({ user: 7, filters: new Set(['active', 'new']) });
```
## ๐จ Benchmark
- **simple** a complete nested object, with an arrays and other mixed primitives.
- **deep** a long, narrow nesting chain.
- **deep circular** the deep chain, but every node points back and the tail closes the loop.
- **big** a wide object: hundreds of keys, but with small values.
- **leafy** a large flat array of mixed primitives.
```
โ simple ~ 1.1ยตs @ 945,104 ops/sec ยฑ 0.16%
โ deep ~ 8.2ยตs @ 121,541 ops/sec ยฑ 1.34%
โ deep circular ~ 5.3ยตs @ 187,820 ops/sec ยฑ 0.18%
โ big ~ 80.2ยตs @ 12,476 ops/sec ยฑ 0.12%
โ leafy ~ 23.0ยตs @ 43,393 ops/sec ยฑ 0.74%
```
All candidates
```
simple
โ object-identity ~ 1.1ยตs @ 945,104 ops/sec ยฑ 0.16%
โ safe-stable-stringify ~ 1.7ยตs @ 597,318 ops/sec ยฑ 0.12%
โ ohash ~ 3.5ยตs @ 285,053 ops/sec ยฑ 0.16%
โ object-hash ~ 9.7ยตs @ 102,648 ops/sec ยฑ 1.63%
โ hash-it โ ~ 2.7ยตs @ 364,144 ops/sec ยฑ 2.48%
โ json-stable-stringify ~ 3.0ยตs @ 329,228 ops/sec ยฑ 0.03%
โ fast-json-stable-stringify ~ 2.0ยตs @ 491,338 ops/sec ยฑ 0.16%
โ tiny-stable-stringify ~ 2.3ยตs @ 432,540 ops/sec ยฑ 0.07%
โ json-stringify-deterministic ~ 3.5ยตs @ 285,498 ops/sec ยฑ 1.10%
โ json-stable-stringify-without-jsonify ~ 3.1ยตs @ 321,857 ops/sec ยฑ 2.79%
โ json-sorted-stringify ~ 2.3ยตs @ 428,881 ops/sec ยฑ 1.60%
โ canonicalize ~ 3.4ยตs @ 298,473 ops/sec ยฑ 0.44%
โ @tufjs/canonical-json
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โญ๏ธ object-identity (58.2% faster than safe-stable-stringify)
deep
โ object-identity ~ 8.2ยตs @ 121,541 ops/sec ยฑ 1.34%
โ safe-stable-stringify ~ 13.4ยตs @ 74,884 ops/sec ยฑ 2.03%
โ ohash ~ 26.1ยตs @ 38,307 ops/sec ยฑ 0.25%
โ object-hash ~ 55.8ยตs @ 17,923 ops/sec ยฑ 0.23%
โ hash-it โ ~ 21.3ยตs @ 47,041 ops/sec ยฑ 1.80%
โ json-stable-stringify ~ 23.1ยตs @ 43,253 ops/sec ยฑ 1.96%
โ fast-json-stable-stringify ~ 13.7ยตs @ 72,892 ops/sec ยฑ 1.60%
โ tiny-stable-stringify ~ 16.0ยตs @ 62,547 ops/sec ยฑ 2.16%
โ json-stringify-deterministic ~ 24.5ยตs @ 40,785 ops/sec ยฑ 0.76%
โ json-stable-stringify-without-jsonify ~ 23.0ยตs @ 43,476 ops/sec ยฑ 1.99%
โ json-sorted-stringify ~ 15.3ยตs @ 65,257 ops/sec ยฑ 0.48%
โ canonicalize ~ 27.0ยตs @ 36,974 ops/sec ยฑ 0.78%
โ @tufjs/canonical-json ~ 35.5ยตs @ 28,130 ops/sec ยฑ 0.96%
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โญ๏ธ object-identity (62.3% faster than safe-stable-stringify)
deep circular
โ object-identity ~ 5.3ยตs @ 187,820 ops/sec ยฑ 0.18%
โ safe-stable-stringify ~ 7.2ยตs @ 138,610 ops/sec ยฑ 0.34%
โ ohash ~ 16.4ยตs @ 61,113 ops/sec ยฑ 0.58%
โ object-hash ~ 44.5ยตs @ 22,492 ops/sec ยฑ 0.37%
โ hash-it โ ~ 15.2ยตs @ 65,867 ops/sec ยฑ 0.10%
โ json-stable-stringify
โ fast-json-stable-stringify
โ tiny-stable-stringify
โ json-stringify-deterministic
โ json-stable-stringify-without-jsonify
โ json-sorted-stringify
โ canonicalize
โ @tufjs/canonical-json
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โญ๏ธ object-identity (35.5% faster than safe-stable-stringify)
big
โ object-identity ~ 80.2ยตs @ 12,476 ops/sec ยฑ 0.12%
โ safe-stable-stringify ~ 134.8ยตs @ 7,418 ops/sec ยฑ 0.70%
โ ohash ~ 288.8ยตs @ 3,463 ops/sec ยฑ 0.30%
โ object-hash ~ 488.0ยตs @ 2,049 ops/sec ยฑ 1.21%
โ hash-it โ ~ 258.4ยตs @ 3,870 ops/sec ยฑ 0.01%
โ json-stable-stringify ~ 212.1ยตs @ 4,715 ops/sec ยฑ 0.26%
โ fast-json-stable-stringify ~ 155.9ยตs @ 6,416 ops/sec ยฑ 0.44%
โ tiny-stable-stringify ~ 165.5ยตs @ 6,043 ops/sec ยฑ 0.27%
โ json-stringify-deterministic ~ 261.1ยตs @ 3,830 ops/sec ยฑ 0.14%
โ json-stable-stringify-without-jsonify ~ 213.3ยตs @ 4,688 ops/sec ยฑ 0.04%
โ json-sorted-stringify ~ 169.8ยตs @ 5,888 ops/sec ยฑ 1.58%
โ canonicalize ~ 256.6ยตs @ 3,897 ops/sec ยฑ 0.13%
โ @tufjs/canonical-json ~ 371.5ยตs @ 2,692 ops/sec ยฑ 0.09%
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โญ๏ธ object-identity (68.2% faster than safe-stable-stringify)
leafy
โ object-identity ~ 23.0ยตs @ 43,393 ops/sec ยฑ 0.74%
โ safe-stable-stringify ~ 49.0ยตs @ 20,402 ops/sec ยฑ 0.54%
โ ohash ~ 30.7ยตs @ 32,569 ops/sec ยฑ 2.43%
โ object-hash ~ 83.9ยตs @ 11,922 ops/sec ยฑ 2.53%
โ hash-it โ ~ 81.8ยตs @ 12,230 ops/sec ยฑ 1.05%
โ json-stable-stringify ~ 112.2ยตs @ 8,912 ops/sec ยฑ 1.22%
โ fast-json-stable-stringify ~ 56.2ยตs @ 17,781 ops/sec ยฑ 1.54%
โ tiny-stable-stringify ~ 106.3ยตs @ 9,410 ops/sec ยฑ 1.94%
โ json-stringify-deterministic ~ 125.3ยตs @ 7,982 ops/sec ยฑ 0.41%
โ json-stable-stringify-without-jsonify ~ 112.4ยตs @ 8,895 ops/sec ยฑ 2.44%
โ json-sorted-stringify ~ 102.7ยตs @ 9,740 ops/sec ยฑ 0.12%
โ canonicalize ~ 89.0ยตs @ 11,239 ops/sec ยฑ 0.67%
โ @tufjs/canonical-json ~ 208.9ยตs @ 4,786 ops/sec ยฑ 0.50%
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โญ๏ธ object-identity (33.2% faster than ohash)
```
> `object-identity` only handles mainly JSON-like data by design. It won't fingerprint functions or
> every Node builtin the way some alternatives do, so these numbers only reflect the payloads it
> targets.
## License
MIT ยฉ [Marais Rossouw](https://marais.io)