# object-identity [![licenses](https://licenses.dev/b/npm/object-identity?style=dark)](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 โค๏ธ [![sponsor me](https://badgen.net/badge/icon/sponsor?icon=github&label&color=gray)](https://github.com/sponsors/maraisr) [![buy me a coffee](https://badgen.net/badge/icon/buymeacoffee?icon=buymeacoffee&label&color=gray)](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 @ 921,139 ops/sec ยฑ 1.00% โœ” deep ~ 8.0ยตs @ 125,088 ops/sec ยฑ 0.38% โœ” deep circular ~ 5.5ยตs @ 182,804 ops/sec ยฑ 3.19% โœ” big ~ 85.1ยตs @ 11,753 ops/sec ยฑ 1.08% โœ” leafy ~ 24.2ยตs @ 41,307 ops/sec ยฑ 0.17% ```
All candidates ``` simple โœ” object-identity ~ 1.1ยตs @ 921,139 ops/sec ยฑ 1.00% โœ” safe-stable-stringify ~ 1.8ยตs @ 560,158 ops/sec ยฑ 0.91% โœ” ohash ~ 3.6ยตs @ 280,116 ops/sec ยฑ 3.31% โœ” object-hash ~ 10.0ยตs @ 99,840 ops/sec ยฑ 0.57% โœ” hash-it โ€  ~ 2.9ยตs @ 342,544 ops/sec ยฑ 2.11% โœ” json-stable-stringify ~ 3.1ยตs @ 326,547 ops/sec ยฑ 0.03% โœ” fast-json-stable-stringify ~ 2.0ยตs @ 488,763 ops/sec ยฑ 0.31% โœ” tiny-stable-stringify ~ 2.4ยตs @ 413,527 ops/sec ยฑ 2.38% โœ” json-stringify-deterministic ~ 3.4ยตs @ 291,605 ops/sec ยฑ 2.59% โœ” json-stable-stringify-without-jsonify ~ 3.1ยตs @ 325,385 ops/sec ยฑ 3.23% โœ” json-sorted-stringify ~ 2.4ยตs @ 412,511 ops/sec ยฑ 1.40% โœ” canonicalize ~ 3.4ยตs @ 296,935 ops/sec ยฑ 0.24% โœ˜ @tufjs/canonical-json โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โญ๏ธŽ object-identity (1.64x faster than safe-stable-stringify) deep โœ” object-identity ~ 8.0ยตs @ 125,088 ops/sec ยฑ 0.38% โœ” safe-stable-stringify ~ 12.9ยตs @ 77,399 ops/sec ยฑ 1.32% โœ” ohash ~ 24.7ยตs @ 40,429 ops/sec ยฑ 0.95% โœ” object-hash ~ 53.3ยตs @ 18,759 ops/sec ยฑ 1.67% โœ” hash-it โ€  ~ 21.1ยตs @ 47,501 ops/sec ยฑ 1.24% โœ” json-stable-stringify ~ 22.8ยตs @ 43,850 ops/sec ยฑ 1.34% โœ” fast-json-stable-stringify ~ 13.6ยตs @ 73,448 ops/sec ยฑ 0.84% โœ” tiny-stable-stringify ~ 16.5ยตs @ 60,768 ops/sec ยฑ 4.79% โœ” json-stringify-deterministic ~ 23.3ยตs @ 42,915 ops/sec ยฑ 1.65% โœ” json-stable-stringify-without-jsonify ~ 22.4ยตs @ 44,583 ops/sec ยฑ 1.17% โœ” json-sorted-stringify ~ 15.4ยตs @ 64,737 ops/sec ยฑ 0.73% โœ” canonicalize ~ 25.4ยตs @ 39,367 ops/sec ยฑ 1.58% โœ” @tufjs/canonical-json ~ 33.7ยตs @ 29,649 ops/sec ยฑ 1.79% โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โญ๏ธŽ object-identity (1.62x faster than safe-stable-stringify) deep circular โœ” object-identity ~ 5.5ยตs @ 182,804 ops/sec ยฑ 3.19% โœ” safe-stable-stringify ~ 7.3ยตs @ 136,207 ops/sec ยฑ 2.78% โœ” ohash ~ 16.3ยตs @ 61,293 ops/sec ยฑ 0.40% โœ” object-hash ~ 44.6ยตs @ 22,433 ops/sec ยฑ 1.20% โœ” hash-it โ€  ~ 15.3ยตs @ 65,287 ops/sec ยฑ 0.38% โœ˜ 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 (1.34x faster than safe-stable-stringify) big โœ” object-identity ~ 85.1ยตs @ 11,753 ops/sec ยฑ 1.08% โœ” safe-stable-stringify ~ 140.5ยตs @ 7,119 ops/sec ยฑ 0.17% โœ” ohash ~ 294.4ยตs @ 3,397 ops/sec ยฑ 0.81% โœ” object-hash ~ 499.8ยตs @ 2,001 ops/sec ยฑ 1.02% โœ” hash-it โ€  ~ 272.9ยตs @ 3,665 ops/sec ยฑ 0.90% โœ” json-stable-stringify ~ 220.2ยตs @ 4,541 ops/sec ยฑ 1.63% โœ” fast-json-stable-stringify ~ 155.9ยตs @ 6,415 ops/sec ยฑ 0.69% โœ” tiny-stable-stringify ~ 165.3ยตs @ 6,050 ops/sec ยฑ 0.02% โœ” json-stringify-deterministic ~ 262.1ยตs @ 3,815 ops/sec ยฑ 0.12% โœ” json-stable-stringify-without-jsonify ~ 221.9ยตs @ 4,506 ops/sec ยฑ 0.79% โœ” json-sorted-stringify ~ 173.8ยตs @ 5,754 ops/sec ยฑ 0.04% โœ” canonicalize ~ 248.1ยตs @ 4,030 ops/sec ยฑ 0.24% โœ” @tufjs/canonical-json ~ 369.1ยตs @ 2,709 ops/sec ยฑ 0.02% โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โญ๏ธŽ object-identity (1.65x faster than safe-stable-stringify) leafy โœ” object-identity ~ 24.2ยตs @ 41,307 ops/sec ยฑ 0.17% โœ” safe-stable-stringify ~ 50.7ยตs @ 19,733 ops/sec ยฑ 0.26% โœ” ohash ~ 30.4ยตs @ 32,871 ops/sec ยฑ 1.66% โœ” object-hash ~ 82.7ยตs @ 12,096 ops/sec ยฑ 0.04% โœ” hash-it โ€  ~ 80.2ยตs @ 12,467 ops/sec ยฑ 0.62% โœ” json-stable-stringify ~ 108.4ยตs @ 9,227 ops/sec ยฑ 0.08% โœ” fast-json-stable-stringify ~ 56.5ยตs @ 17,685 ops/sec ยฑ 1.11% โœ” tiny-stable-stringify ~ 103.3ยตs @ 9,678 ops/sec ยฑ 0.41% โœ” json-stringify-deterministic ~ 121.2ยตs @ 8,248 ops/sec ยฑ 0.13% โœ” json-stable-stringify-without-jsonify ~ 111.9ยตs @ 8,936 ops/sec ยฑ 0.12% โœ” json-sorted-stringify ~ 103.9ยตs @ 9,626 ops/sec ยฑ 0.35% โœ” canonicalize ~ 90.2ยตs @ 11,085 ops/sec ยฑ 0.15% โœ” @tufjs/canonical-json ~ 209.4ยตs @ 4,776 ops/sec ยฑ 0.34% โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โญ๏ธŽ object-identity (1.26x 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)