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 535B 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         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-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