maraisr/object-identityPublic

readme.md

object-identity licenses

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 buy me a coffee

โšก 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

๐Ÿš€ 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 @ 945,104 ops/sec ยฑ 0.16%
โœ” deep          ~  8.2ยตs @ 121,541 ops/sec ยฑ 1.34%
โœ” deep circular ~  5.3ยตs @ 187,820 ops/sec ยฑ 0.18%
โœ” big           ~ 80.2ยตs @  12,476 ops/sec ยฑ 0.12%
โœ” leafy         ~ 23.0ยตs @  43,393 ops/sec ยฑ 0.74%
All candidates
simple
โœ” object-identity                       ~ 1.1ยตs @ 945,104 ops/sec ยฑ 0.16%
โœ” safe-stable-stringify                 ~ 1.7ยตs @ 597,318 ops/sec ยฑ 0.12%
โœ” ohash                                 ~ 3.5ยตs @ 285,053 ops/sec ยฑ 0.16%
โœ” object-hash                           ~ 9.7ยตs @ 102,648 ops/sec ยฑ 1.63%
โœ” hash-it โ€                              ~ 2.7ยตs @ 364,144 ops/sec ยฑ 2.48%
โœ” json-stable-stringify                 ~ 3.0ยตs @ 329,228 ops/sec ยฑ 0.03%
โœ” fast-json-stable-stringify            ~ 2.0ยตs @ 491,338 ops/sec ยฑ 0.16%
โœ” tiny-stable-stringify                 ~ 2.3ยตs @ 432,540 ops/sec ยฑ 0.07%
โœ” json-stringify-deterministic          ~ 3.5ยตs @ 285,498 ops/sec ยฑ 1.10%
โœ” json-stable-stringify-without-jsonify ~ 3.1ยตs @ 321,857 ops/sec ยฑ 2.79%
โœ” json-sorted-stringify                 ~ 2.3ยตs @ 428,881 ops/sec ยฑ 1.60%
โœ” canonicalize                          ~ 3.4ยตs @ 298,473 ops/sec ยฑ 0.44%
โœ˜ @tufjs/canonical-json
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โญ๏ธŽ object-identity (58.2% faster than safe-stable-stringify)

deep
โœ” object-identity                       ~  8.2ยตs @ 121,541 ops/sec ยฑ 1.34%
โœ” safe-stable-stringify                 ~ 13.4ยตs @  74,884 ops/sec ยฑ 2.03%
โœ” ohash                                 ~ 26.1ยตs @  38,307 ops/sec ยฑ 0.25%
โœ” object-hash                           ~ 55.8ยตs @  17,923 ops/sec ยฑ 0.23%
โœ” hash-it โ€                              ~ 21.3ยตs @  47,041 ops/sec ยฑ 1.80%
โœ” json-stable-stringify                 ~ 23.1ยตs @  43,253 ops/sec ยฑ 1.96%
โœ” fast-json-stable-stringify            ~ 13.7ยตs @  72,892 ops/sec ยฑ 1.60%
โœ” tiny-stable-stringify                 ~ 16.0ยตs @  62,547 ops/sec ยฑ 2.16%
โœ” json-stringify-deterministic          ~ 24.5ยตs @  40,785 ops/sec ยฑ 0.76%
โœ” json-stable-stringify-without-jsonify ~ 23.0ยตs @  43,476 ops/sec ยฑ 1.99%
โœ” json-sorted-stringify                 ~ 15.3ยตs @  65,257 ops/sec ยฑ 0.48%
โœ” canonicalize                          ~ 27.0ยตs @  36,974 ops/sec ยฑ 0.78%
โœ” @tufjs/canonical-json                 ~ 35.5ยตs @  28,130 ops/sec ยฑ 0.96%
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โญ๏ธŽ object-identity (62.3% faster than safe-stable-stringify)

deep circular
โœ” object-identity                       ~  5.3ยตs @ 187,820 ops/sec ยฑ 0.18%
โœ” safe-stable-stringify                 ~  7.2ยตs @ 138,610 ops/sec ยฑ 0.34%
โœ” ohash                                 ~ 16.4ยตs @  61,113 ops/sec ยฑ 0.58%
โœ” object-hash                           ~ 44.5ยตs @  22,492 ops/sec ยฑ 0.37%
โœ” hash-it โ€                              ~ 15.2ยตs @  65,867 ops/sec ยฑ 0.10%
โœ˜ 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 (35.5% faster than safe-stable-stringify)

big
โœ” object-identity                       ~  80.2ยตs @ 12,476 ops/sec ยฑ 0.12%
โœ” safe-stable-stringify                 ~ 134.8ยตs @  7,418 ops/sec ยฑ 0.70%
โœ” ohash                                 ~ 288.8ยตs @  3,463 ops/sec ยฑ 0.30%
โœ” object-hash                           ~ 488.0ยตs @  2,049 ops/sec ยฑ 1.21%
โœ” hash-it โ€                              ~ 258.4ยตs @  3,870 ops/sec ยฑ 0.01%
โœ” json-stable-stringify                 ~ 212.1ยตs @  4,715 ops/sec ยฑ 0.26%
โœ” fast-json-stable-stringify            ~ 155.9ยตs @  6,416 ops/sec ยฑ 0.44%
โœ” tiny-stable-stringify                 ~ 165.5ยตs @  6,043 ops/sec ยฑ 0.27%
โœ” json-stringify-deterministic          ~ 261.1ยตs @  3,830 ops/sec ยฑ 0.14%
โœ” json-stable-stringify-without-jsonify ~ 213.3ยตs @  4,688 ops/sec ยฑ 0.04%
โœ” json-sorted-stringify                 ~ 169.8ยตs @  5,888 ops/sec ยฑ 1.58%
โœ” canonicalize                          ~ 256.6ยตs @  3,897 ops/sec ยฑ 0.13%
โœ” @tufjs/canonical-json                 ~ 371.5ยตs @  2,692 ops/sec ยฑ 0.09%
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โญ๏ธŽ object-identity (68.2% faster than safe-stable-stringify)

leafy
โœ” object-identity                       ~  23.0ยตs @ 43,393 ops/sec ยฑ 0.74%
โœ” safe-stable-stringify                 ~  49.0ยตs @ 20,402 ops/sec ยฑ 0.54%
โœ” ohash                                 ~  30.7ยตs @ 32,569 ops/sec ยฑ 2.43%
โœ” object-hash                           ~  83.9ยตs @ 11,922 ops/sec ยฑ 2.53%
โœ” hash-it โ€                              ~  81.8ยตs @ 12,230 ops/sec ยฑ 1.05%
โœ” json-stable-stringify                 ~ 112.2ยตs @  8,912 ops/sec ยฑ 1.22%
โœ” fast-json-stable-stringify            ~  56.2ยตs @ 17,781 ops/sec ยฑ 1.54%
โœ” tiny-stable-stringify                 ~ 106.3ยตs @  9,410 ops/sec ยฑ 1.94%
โœ” json-stringify-deterministic          ~ 125.3ยตs @  7,982 ops/sec ยฑ 0.41%
โœ” json-stable-stringify-without-jsonify ~ 112.4ยตs @  8,895 ops/sec ยฑ 2.44%
โœ” json-sorted-stringify                 ~ 102.7ยตs @  9,740 ops/sec ยฑ 0.12%
โœ” canonicalize                          ~  89.0ยตs @ 11,239 ops/sec ยฑ 0.67%
โœ” @tufjs/canonical-json                 ~ 208.9ยตs @  4,786 ops/sec ยฑ 0.50%
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โญ๏ธŽ object-identity (33.2% 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