# 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 555B 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 @ 905,989 ops/sec ยฑ 0.75%
โ deep ~ 9.4ยตs @ 106,803 ops/sec ยฑ 0.50%
โ deep circular ~ 5.9ยตs @ 169,953 ops/sec ยฑ 1.28%
โ big ~ 140.0ยตs @ 7,143 ops/sec ยฑ 0.81%
โ leafy ~ 23.6ยตs @ 42,430 ops/sec ยฑ 0.59%
```
All candidates
```
simple
โ object-identity ~ 1.1ยตs @ 905,989 ops/sec ยฑ 0.75%
โ safe-stable-stringify ~ 1.7ยตs @ 578,151 ops/sec ยฑ 0.40%
โ ohash ~ 3.5ยตs @ 285,422 ops/sec ยฑ 0.19%
โ object-hash ~ 9.7ยตs @ 103,295 ops/sec ยฑ 1.45%
โ hash-it โ ~ 2.9ยตs @ 341,608 ops/sec ยฑ 1.55%
โ json-stable-stringify ~ 3.0ยตs @ 329,697 ops/sec ยฑ 0.20%
โ fast-json-stable-stringify ~ 2.0ยตs @ 495,893 ops/sec ยฑ 0.52%
โ tiny-stable-stringify ~ 2.4ยตs @ 424,881 ops/sec ยฑ 0.52%
โ json-stringify-deterministic ~ 3.3ยตs @ 303,377 ops/sec ยฑ 0.08%
โ json-stable-stringify-without-jsonify ~ 3.0ยตs @ 329,644 ops/sec ยฑ 0.61%
โ json-sorted-stringify ~ 2.4ยตs @ 417,033 ops/sec ยฑ 2.05%
โ canonicalize ~ 3.5ยตs @ 283,645 ops/sec ยฑ 0.90%
โ @tufjs/canonical-json
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โญ๏ธ object-identity (56.7% faster than safe-stable-stringify)
deep
โ object-identity ~ 9.4ยตs @ 106,803 ops/sec ยฑ 0.50%
โ safe-stable-stringify ~ 13.6ยตs @ 73,421 ops/sec ยฑ 3.02%
โ ohash ~ 27.0ยตs @ 37,063 ops/sec ยฑ 6.73%
โ object-hash ~ 51.2ยตs @ 19,547 ops/sec ยฑ 2.02%
โ hash-it โ ~ 21.6ยตs @ 46,238 ops/sec ยฑ 2.52%
โ json-stable-stringify ~ 22.8ยตs @ 43,806 ops/sec ยฑ 2.16%
โ fast-json-stable-stringify ~ 13.6ยตs @ 73,774 ops/sec ยฑ 1.64%
โ tiny-stable-stringify ~ 15.6ยตs @ 64,214 ops/sec ยฑ 1.00%
โ json-stringify-deterministic ~ 23.1ยตs @ 43,210 ops/sec ยฑ 1.88%
โ json-stable-stringify-without-jsonify ~ 22.5ยตs @ 44,512 ops/sec ยฑ 1.70%
โ json-sorted-stringify ~ 15.4ยตs @ 64,922 ops/sec ยฑ 0.99%
โ canonicalize ~ 25.2ยตs @ 39,606 ops/sec ยฑ 1.98%
โ @tufjs/canonical-json ~ 34.1ยตs @ 29,287 ops/sec ยฑ 2.38%
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โญ๏ธ object-identity (44.8% faster than fast-json-stable-stringify)
deep circular
โ object-identity ~ 5.9ยตs @ 169,953 ops/sec ยฑ 1.28%
โ safe-stable-stringify ~ 7.3ยตs @ 136,608 ops/sec ยฑ 0.02%
โ ohash ~ 16.4ยตs @ 60,983 ops/sec ยฑ 0.40%
โ object-hash ~ 42.4ยตs @ 23,583 ops/sec ยฑ 1.25%
โ hash-it โ ~ 15.1ยตs @ 66,432 ops/sec ยฑ 1.18%
โ 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 (24.4% faster than safe-stable-stringify)
big
โ object-identity ~ 140.0ยตs @ 7,143 ops/sec ยฑ 0.81%
โ safe-stable-stringify ~ 137.5ยตs @ 7,274 ops/sec ยฑ 1.89%
โ ohash ~ 288.2ยตs @ 3,470 ops/sec ยฑ 0.51%
โ object-hash ~ 479.9ยตs @ 2,084 ops/sec ยฑ 1.63%
โ hash-it โ ~ 277.3ยตs @ 3,606 ops/sec ยฑ 0.50%
โ json-stable-stringify ~ 227.2ยตs @ 4,402 ops/sec ยฑ 3.02%
โ fast-json-stable-stringify ~ 150.4ยตs @ 6,650 ops/sec ยฑ 0.98%
โ tiny-stable-stringify ~ 164.3ยตs @ 6,086 ops/sec ยฑ 0.27%
โ json-stringify-deterministic ~ 255.3ยตs @ 3,917 ops/sec ยฑ 0.51%
โ json-stable-stringify-without-jsonify ~ 209.2ยตs @ 4,780 ops/sec ยฑ 0.39%
โ json-sorted-stringify ~ 163.7ยตs @ 6,111 ops/sec ยฑ 0.32%
โ canonicalize ~ 239.8ยตs @ 4,171 ops/sec ยฑ 0.83%
โ @tufjs/canonical-json ~ 364.4ยตs @ 2,744 ops/sec ยฑ 0.63%
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โญ๏ธ safe-stable-stringify (1.8% faster than object-identity)
leafy
โ object-identity ~ 23.6ยตs @ 42,430 ops/sec ยฑ 0.59%
โ safe-stable-stringify ~ 51.0ยตs @ 19,595 ops/sec ยฑ 0.26%
โ ohash ~ 31.8ยตs @ 31,478 ops/sec ยฑ 1.37%
โ object-hash ~ 85.6ยตs @ 11,682 ops/sec ยฑ 0.12%
โ hash-it โ ~ 83.6ยตs @ 11,967 ops/sec ยฑ 0.17%
โ json-stable-stringify ~ 104.6ยตs @ 9,556 ops/sec ยฑ 0.66%
โ fast-json-stable-stringify ~ 54.4ยตs @ 18,386 ops/sec ยฑ 1.25%
โ tiny-stable-stringify ~ 101.9ยตs @ 9,816 ops/sec ยฑ 0.13%
โ json-stringify-deterministic ~ 118.3ยตs @ 8,453 ops/sec ยฑ 0.51%
โ json-stable-stringify-without-jsonify ~ 108.6ยตs @ 9,211 ops/sec ยฑ 0.45%
โ json-sorted-stringify ~ 101.7ยตs @ 9,829 ops/sec ยฑ 0.13%
โ canonicalize ~ 86.6ยตs @ 11,552 ops/sec ยฑ 0.85%
โ @tufjs/canonical-json ~ 203.7ยตs @ 4,909 ops/sec ยฑ 1.25%
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โญ๏ธ object-identity (34.8% 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)