# 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 529B 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 @ 932,332 ops/sec ยฑ 0.76% โœ” deep ~ 8.0ยตs @ 124,505 ops/sec ยฑ 2.85% โœ” deep circular ~ 5.5ยตs @ 183,042 ops/sec ยฑ 0.16% โœ” big ~ 80.2ยตs @ 12,467 ops/sec ยฑ 0.42% โœ” leafy ~ 23.7ยตs @ 42,223 ops/sec ยฑ 0.63% ```
All candidates ``` simple โœ” object-identity ~ 1.1ยตs @ 932,332 ops/sec ยฑ 0.76% โœ” safe-stable-stringify ~ 1.7ยตs @ 588,871 ops/sec ยฑ 0.67% โœ” ohash ~ 3.5ยตs @ 285,750 ops/sec ยฑ 0.28% โœ” object-hash ~ 9.7ยตs @ 102,617 ops/sec ยฑ 2.08% โœ” hash-it โ€  ~ 2.8ยตs @ 361,547 ops/sec ยฑ 2.62% โœ” json-stable-stringify ~ 3.1ยตs @ 325,737 ops/sec ยฑ 0.08% โœ” fast-json-stable-stringify ~ 2.1ยตs @ 480,206 ops/sec ยฑ 0.44% โœ” tiny-stable-stringify ~ 2.4ยตs @ 409,949 ops/sec ยฑ 1.60% โœ” json-stringify-deterministic ~ 3.1ยตs @ 326,067 ops/sec ยฑ 2.55% โœ” json-stable-stringify-without-jsonify ~ 3.0ยตs @ 329,660 ops/sec ยฑ 0.25% โœ” json-sorted-stringify ~ 2.3ยตs @ 429,194 ops/sec ยฑ 0.54% โœ” canonicalize ~ 3.5ยตs @ 284,416 ops/sec ยฑ 1.62% โœ˜ @tufjs/canonical-json โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โญ๏ธŽ object-identity (1.58x faster than safe-stable-stringify) deep โœ” object-identity ~ 8.0ยตs @ 124,505 ops/sec ยฑ 2.85% โœ” safe-stable-stringify ~ 13.6ยตs @ 73,292 ops/sec ยฑ 0.45% โœ” ohash ~ 25.6ยตs @ 39,078 ops/sec ยฑ 0.79% โœ” object-hash ~ 53.3ยตs @ 18,748 ops/sec ยฑ 1.73% โœ” hash-it โ€  ~ 21.1ยตs @ 47,506 ops/sec ยฑ 1.23% โœ” json-stable-stringify ~ 22.9ยตs @ 43,649 ops/sec ยฑ 1.24% โœ” fast-json-stable-stringify ~ 14.4ยตs @ 69,262 ops/sec ยฑ 0.14% โœ” tiny-stable-stringify ~ 16.4ยตs @ 60,994 ops/sec ยฑ 0.38% โœ” json-stringify-deterministic ~ 22.5ยตs @ 44,468 ops/sec ยฑ 1.25% โœ” json-stable-stringify-without-jsonify ~ 22.7ยตs @ 43,964 ops/sec ยฑ 0.53% โœ” json-sorted-stringify ~ 16.1ยตs @ 61,935 ops/sec ยฑ 0.64% โœ” canonicalize ~ 26.7ยตs @ 37,508 ops/sec ยฑ 0.76% โœ” @tufjs/canonical-json ~ 34.5ยตs @ 29,020 ops/sec ยฑ 1.45% โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โญ๏ธŽ object-identity (1.70x faster than safe-stable-stringify) deep circular โœ” object-identity ~ 5.5ยตs @ 183,042 ops/sec ยฑ 0.16% โœ” safe-stable-stringify ~ 7.2ยตs @ 139,264 ops/sec ยฑ 0.32% โœ” ohash ~ 16.6ยตs @ 60,357 ops/sec ยฑ 0.66% โœ” object-hash ~ 44.1ยตs @ 22,688 ops/sec ยฑ 1.50% โœ” hash-it โ€  ~ 15.1ยตs @ 66,295 ops/sec ยฑ 1.39% โœ˜ json-stable-stringify โœ” fast-json-stable-stringify ~ 10.0ยตs @ 100,160 ops/sec ยฑ 0.67% โœ˜ tiny-stable-stringify โœ” json-stringify-deterministic ~ 14.8ยตs @ 67,354 ops/sec ยฑ 1.50% โœ˜ json-stable-stringify-without-jsonify โœ˜ json-sorted-stringify โœ˜ canonicalize โœ˜ @tufjs/canonical-json โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โญ๏ธŽ object-identity (1.31x faster than safe-stable-stringify) big โœ” object-identity ~ 80.2ยตs @ 12,467 ops/sec ยฑ 0.42% โœ” safe-stable-stringify ~ 135.1ยตs @ 7,404 ops/sec ยฑ 0.88% โœ” ohash ~ 289.9ยตs @ 3,449 ops/sec ยฑ 0.63% โœ” object-hash ~ 490.0ยตs @ 2,041 ops/sec ยฑ 1.51% โœ” hash-it โ€  ~ 258.9ยตs @ 3,862 ops/sec ยฑ 0.48% โœ” json-stable-stringify ~ 214.5ยตs @ 4,662 ops/sec ยฑ 0.69% โœ” fast-json-stable-stringify ~ 156.8ยตs @ 6,379 ops/sec ยฑ 0.69% โœ” tiny-stable-stringify ~ 174.3ยตs @ 5,736 ops/sec ยฑ 0.24% โœ” json-stringify-deterministic ~ 223.6ยตs @ 4,471 ops/sec ยฑ 0.38% โœ” json-stable-stringify-without-jsonify ~ 215.6ยตs @ 4,639 ops/sec ยฑ 0.21% โœ” json-sorted-stringify ~ 165.3ยตs @ 6,049 ops/sec ยฑ 0.13% โœ” canonicalize ~ 245.5ยตs @ 4,073 ops/sec ยฑ 0.44% โœ” @tufjs/canonical-json ~ 369.7ยตs @ 2,705 ops/sec ยฑ 0.11% โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โญ๏ธŽ object-identity (1.68x faster than safe-stable-stringify) leafy โœ” object-identity ~ 23.7ยตs @ 42,223 ops/sec ยฑ 0.63% โœ” safe-stable-stringify ~ 51.0ยตs @ 19,622 ops/sec ยฑ 0.66% โœ” ohash ~ 30.6ยตs @ 32,645 ops/sec ยฑ 0.38% โœ” object-hash ~ 82.1ยตs @ 12,183 ops/sec ยฑ 0.10% โœ” hash-it โ€  ~ 83.0ยตs @ 12,050 ops/sec ยฑ 0.42% โœ” json-stable-stringify ~ 113.6ยตs @ 8,800 ops/sec ยฑ 0.02% โœ” fast-json-stable-stringify ~ 59.2ยตs @ 16,901 ops/sec ยฑ 1.06% โœ” tiny-stable-stringify ~ 103.5ยตs @ 9,661 ops/sec ยฑ 0.11% โœ” json-stringify-deterministic ~ 111.5ยตs @ 8,965 ops/sec ยฑ 0.00% โœ” json-stable-stringify-without-jsonify ~ 113.7ยตs @ 8,796 ops/sec ยฑ 2.00% โœ” json-sorted-stringify ~ 107.3ยตs @ 9,324 ops/sec ยฑ 0.97% โœ” canonicalize ~ 92.4ยตs @ 10,821 ops/sec ยฑ 1.36% โœ” @tufjs/canonical-json ~ 207.2ยตs @ 4,826 ops/sec ยฑ 0.72% โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โญ๏ธŽ object-identity (1.29x 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)