- .github
- lib
- scripts
- .gitignore38 B
- deno.json787 B
- license1.1 KB
- package.json871 B
- readme.md6.1 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 535B 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 829,712 ops/sec
โ deep 117,578 ops/sec
โ deep circular 169,652 ops/sec
โ big 11,664 ops/sec
โ leafy 41,925 ops/sec
All candidates
simple
+ โ object-identity 829,712 ops/sec
โ safe-stable-stringify 544,561 ops/sec 1.52x
โ ohash 257,343 ops/sec 3.22x
โ object-hash 96,890 ops/sec 8.56x
โ hash-it โ 328,222 ops/sec 2.53x
โ json-stable-stringify 309,313 ops/sec 2.68x
โ fast-json-stable-stringify 458,943 ops/sec 1.81x
โ tiny-stable-stringify 402,523 ops/sec 2.06x
โ json-stringify-deterministic 311,265 ops/sec 2.67x
โ json-sorted-stringify 402,961 ops/sec 2.06x
โ canonicalize 267,754 ops/sec 3.10x
โ swr/_internal/stableHash 250,439 ops/sec 3.31x
โ @tufjs/canonical-json
deep
+ โ object-identity 117,578 ops/sec
โ safe-stable-stringify 74,688 ops/sec 1.57x
โ ohash 38,360 ops/sec 3.07x
โ object-hash 17,877 ops/sec 6.58x
โ hash-it โ 44,260 ops/sec 2.66x
โ json-stable-stringify 41,899 ops/sec 2.81x
โ fast-json-stable-stringify 70,452 ops/sec 1.67x
โ tiny-stable-stringify 62,336 ops/sec 1.89x
โ json-stringify-deterministic 43,571 ops/sec 2.70x
โ json-sorted-stringify 62,523 ops/sec 1.88x
โ canonicalize 37,471 ops/sec 3.14x
โ swr/_internal/stableHash 34,445 ops/sec 3.41x
โ @tufjs/canonical-json 28,960 ops/sec 4.06x
deep circular
+ โ object-identity 169,652 ops/sec
โ safe-stable-stringify 133,794 ops/sec 1.27x
โ ohash 58,065 ops/sec 2.92x
โ object-hash 22,029 ops/sec 7.70x
โ hash-it โ 62,480 ops/sec 2.72x
โ json-stable-stringify
โ fast-json-stable-stringify 94,420 ops/sec 1.80x
โ tiny-stable-stringify
โ json-stringify-deterministic 62,684 ops/sec 2.71x
โ json-sorted-stringify
โ canonicalize
โ swr/_internal/stableHash 54,642 ops/sec 3.10x
โ @tufjs/canonical-json
big
+ โ object-identity 11,664 ops/sec
โ safe-stable-stringify 7,097 ops/sec 1.64x
โ ohash 3,287 ops/sec 3.55x
โ object-hash 1,940 ops/sec 6.01x
โ hash-it โ 3,583 ops/sec 3.26x
โ json-stable-stringify 4,396 ops/sec 2.65x
โ fast-json-stable-stringify 6,219 ops/sec 1.88x
โ tiny-stable-stringify 5,771 ops/sec 2.02x
โ json-stringify-deterministic 4,448 ops/sec 2.62x
โ json-sorted-stringify 5,719 ops/sec 2.04x
โ canonicalize 3,799 ops/sec 3.07x
โ swr/_internal/stableHash 3,536 ops/sec 3.30x
โ @tufjs/canonical-json 2,542 ops/sec 4.59x
leafy
+ โ object-identity 41,925 ops/sec
โ safe-stable-stringify 20,088 ops/sec 2.09x
โ ohash 32,586 ops/sec 1.29x
โ object-hash 11,693 ops/sec 3.59x
โ hash-it โ 12,092 ops/sec 3.47x
โ json-stable-stringify 8,863 ops/sec 4.73x
โ fast-json-stable-stringify 16,968 ops/sec 2.47x
โ tiny-stable-stringify 9,269 ops/sec 4.52x
โ json-stringify-deterministic 8,680 ops/sec 4.83x
โ json-sorted-stringify 9,522 ops/sec 4.40x
โ canonicalize 10,858 ops/sec 3.86x
โ swr/_internal/stableHash 7,982 ops/sec 5.25x
โ @tufjs/canonical-json 4,714 ops/sec 8.89x
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