- .github
- lib
- scripts
- .gitignore38 B
- deno.json787 B
- license1.1 KB
- package.json767 B
- readme.md8.6 KB
readme.md
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 โค๏ธ
โก 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.
- ๐ชถ Tiny. Around 538B minified and gzipped, with zero dependencies.
โ๏ธ Install
- npm โ available as
object-identity - JSR โ available as
@mr/object-identity
๐ Usage
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-identityonly 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