/object-identity

#️⃣ Object hashing for structural equality

Primary LanguageTypeScriptMIT LicenseMIT

object-identity licenses

A utility that provides a stable identity of an object



This is free to use software, but if you do like it, consider supporting me ❤️

sponsor me buy me a coffee

⚡ Features

  • Intuitive
  • 🌪 Recursive/Circular support
  • 🏎 Performant — check the benchmarks.
  • 🪶 Lightweight — a mere 387B and no dependencies.

⚙️ Install

🚀 Usage

import { identify } from 'object-identity';

// ~> identity the object
const id1 = identify({ a: new Set(['b', 'c', new Map([['d', 'e']])]) });
// ~> an entirely different object, but structurally the same
const id2 = identify({ a: new Set(['b', 'c', new Map([['e', 'e']])]) });

// they should equal
assert.toEqual(hashA, hashB);

💨 Benchmark

benchmark                  time (avg)        iter/s             (min … max)       p75       p99      p995
--------------------------------------------------------------------------- -----------------------------
object-identity              2.2 µs/iter     453,803.6     (1.99 µs … 2.44 µs) 2.35 µs 2.44 µs 2.44 µs
object-hash                 8.76 µs/iter     114,168.3   (7.96 µs … 225.33 µs) 8.71 µs 11.75 µs 14.92 µs
json-stable-stringify       1.77 µs/iter     565,184.5     (1.75 µs … 1.86 µs) 1.77 µs 1.86 µs 1.86 µs
tiny-stable-stringify       1.63 µs/iter     612,009.4     (1.62 µs … 1.68 µs) 1.64 µs 1.68 µs 1.68 µs

summary
  object-identity
   1.35x slower than tiny-stable-stringify
   1.25x slower than json-stable-stringify
   3.97x faster than object-hash

^ object-identity is not as feature-full it's alternatives, specifically around function values and other node builtins. So take this benchmark with a grain of salt, as it's only testing "json-like" payloads.

License

MIT © Marais Rossouw