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 555B 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 @ 905,989 ops/sec ยฑ 0.75%
โœ” deep          ~   9.4ยตs @ 106,803 ops/sec ยฑ 0.50%
โœ” deep circular ~   5.9ยตs @ 169,953 ops/sec ยฑ 1.28%
โœ” big           ~ 140.0ยตs @   7,143 ops/sec ยฑ 0.81%
โœ” leafy         ~  23.6ยตs @  42,430 ops/sec ยฑ 0.59%
All candidates
simple
โœ” object-identity                       ~ 1.1ยตs @ 905,989 ops/sec ยฑ 0.75%
โœ” safe-stable-stringify                 ~ 1.7ยตs @ 578,151 ops/sec ยฑ 0.40%
โœ” ohash                                 ~ 3.5ยตs @ 285,422 ops/sec ยฑ 0.19%
โœ” object-hash                           ~ 9.7ยตs @ 103,295 ops/sec ยฑ 1.45%
โœ” hash-it โ€                              ~ 2.9ยตs @ 341,608 ops/sec ยฑ 1.55%
โœ” json-stable-stringify                 ~ 3.0ยตs @ 329,697 ops/sec ยฑ 0.20%
โœ” fast-json-stable-stringify            ~ 2.0ยตs @ 495,893 ops/sec ยฑ 0.52%
โœ” tiny-stable-stringify                 ~ 2.4ยตs @ 424,881 ops/sec ยฑ 0.52%
โœ” json-stringify-deterministic          ~ 3.3ยตs @ 303,377 ops/sec ยฑ 0.08%
โœ” json-stable-stringify-without-jsonify ~ 3.0ยตs @ 329,644 ops/sec ยฑ 0.61%
โœ” json-sorted-stringify                 ~ 2.4ยตs @ 417,033 ops/sec ยฑ 2.05%
โœ” canonicalize                          ~ 3.5ยตs @ 283,645 ops/sec ยฑ 0.90%
โœ˜ @tufjs/canonical-json
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โญ๏ธŽ object-identity (56.7% faster than safe-stable-stringify)

deep
โœ” object-identity                       ~  9.4ยตs @ 106,803 ops/sec ยฑ 0.50%
โœ” safe-stable-stringify                 ~ 13.6ยตs @  73,421 ops/sec ยฑ 3.02%
โœ” ohash                                 ~ 27.0ยตs @  37,063 ops/sec ยฑ 6.73%
โœ” object-hash                           ~ 51.2ยตs @  19,547 ops/sec ยฑ 2.02%
โœ” hash-it โ€                              ~ 21.6ยตs @  46,238 ops/sec ยฑ 2.52%
โœ” json-stable-stringify                 ~ 22.8ยตs @  43,806 ops/sec ยฑ 2.16%
โœ” fast-json-stable-stringify            ~ 13.6ยตs @  73,774 ops/sec ยฑ 1.64%
โœ” tiny-stable-stringify                 ~ 15.6ยตs @  64,214 ops/sec ยฑ 1.00%
โœ” json-stringify-deterministic          ~ 23.1ยตs @  43,210 ops/sec ยฑ 1.88%
โœ” json-stable-stringify-without-jsonify ~ 22.5ยตs @  44,512 ops/sec ยฑ 1.70%
โœ” json-sorted-stringify                 ~ 15.4ยตs @  64,922 ops/sec ยฑ 0.99%
โœ” canonicalize                          ~ 25.2ยตs @  39,606 ops/sec ยฑ 1.98%
โœ” @tufjs/canonical-json                 ~ 34.1ยตs @  29,287 ops/sec ยฑ 2.38%
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โญ๏ธŽ object-identity (44.8% faster than fast-json-stable-stringify)

deep circular
โœ” object-identity                       ~  5.9ยตs @ 169,953 ops/sec ยฑ 1.28%
โœ” safe-stable-stringify                 ~  7.3ยตs @ 136,608 ops/sec ยฑ 0.02%
โœ” ohash                                 ~ 16.4ยตs @  60,983 ops/sec ยฑ 0.40%
โœ” object-hash                           ~ 42.4ยตs @  23,583 ops/sec ยฑ 1.25%
โœ” hash-it โ€                              ~ 15.1ยตs @  66,432 ops/sec ยฑ 1.18%
โœ˜ 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 (24.4% faster than safe-stable-stringify)

big
โœ” object-identity                       ~ 140.0ยตs @ 7,143 ops/sec ยฑ 0.81%
โœ” safe-stable-stringify                 ~ 137.5ยตs @ 7,274 ops/sec ยฑ 1.89%
โœ” ohash                                 ~ 288.2ยตs @ 3,470 ops/sec ยฑ 0.51%
โœ” object-hash                           ~ 479.9ยตs @ 2,084 ops/sec ยฑ 1.63%
โœ” hash-it โ€                              ~ 277.3ยตs @ 3,606 ops/sec ยฑ 0.50%
โœ” json-stable-stringify                 ~ 227.2ยตs @ 4,402 ops/sec ยฑ 3.02%
โœ” fast-json-stable-stringify            ~ 150.4ยตs @ 6,650 ops/sec ยฑ 0.98%
โœ” tiny-stable-stringify                 ~ 164.3ยตs @ 6,086 ops/sec ยฑ 0.27%
โœ” json-stringify-deterministic          ~ 255.3ยตs @ 3,917 ops/sec ยฑ 0.51%
โœ” json-stable-stringify-without-jsonify ~ 209.2ยตs @ 4,780 ops/sec ยฑ 0.39%
โœ” json-sorted-stringify                 ~ 163.7ยตs @ 6,111 ops/sec ยฑ 0.32%
โœ” canonicalize                          ~ 239.8ยตs @ 4,171 ops/sec ยฑ 0.83%
โœ” @tufjs/canonical-json                 ~ 364.4ยตs @ 2,744 ops/sec ยฑ 0.63%
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โญ๏ธŽ safe-stable-stringify (1.8% faster than object-identity)

leafy
โœ” object-identity                       ~  23.6ยตs @ 42,430 ops/sec ยฑ 0.59%
โœ” safe-stable-stringify                 ~  51.0ยตs @ 19,595 ops/sec ยฑ 0.26%
โœ” ohash                                 ~  31.8ยตs @ 31,478 ops/sec ยฑ 1.37%
โœ” object-hash                           ~  85.6ยตs @ 11,682 ops/sec ยฑ 0.12%
โœ” hash-it โ€                              ~  83.6ยตs @ 11,967 ops/sec ยฑ 0.17%
โœ” json-stable-stringify                 ~ 104.6ยตs @  9,556 ops/sec ยฑ 0.66%
โœ” fast-json-stable-stringify            ~  54.4ยตs @ 18,386 ops/sec ยฑ 1.25%
โœ” tiny-stable-stringify                 ~ 101.9ยตs @  9,816 ops/sec ยฑ 0.13%
โœ” json-stringify-deterministic          ~ 118.3ยตs @  8,453 ops/sec ยฑ 0.51%
โœ” json-stable-stringify-without-jsonify ~ 108.6ยตs @  9,211 ops/sec ยฑ 0.45%
โœ” json-sorted-stringify                 ~ 101.7ยตs @  9,829 ops/sec ยฑ 0.13%
โœ” canonicalize                          ~  86.6ยตs @ 11,552 ops/sec ยฑ 0.85%
โœ” @tufjs/canonical-json                 ~ 203.7ยตs @  4,909 ops/sec ยฑ 1.25%
โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
โญ๏ธŽ object-identity (34.8% 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