object-identity
#
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 ❤️
⚡ Features#
- ✅ Intuitive
- 🌪 Recursive/Circular support
- 🏎 Performant — check the benchmarks.
- 🪶 Lightweight — a mere 387B and no dependencies.
⚙️ Install#
- npm — available as
object-identity - JSR — available as
@mr/object-identity
🚀 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/iter (avg) | iter/s | (min … max) | p75 | p99 | p995 |
| ----------------------- | --------------- | ------------- | --------------------- | -------- | -------- | -------- |
| object-identity | 573.0 ns | 1,745,000 | (540.1 ns … 607.1 ns) | 585.2 ns | 607.1 ns | 607.1 ns |
| object-hash | 6.7 µs | 149,000 | ( 6.2 µs … 164.9 µs) | 6.5 µs | 8.0 µs | 21.2 µs |
| json-stable-stringify | 1.7 µs | 588,400 | ( 1.6 µs … 2.9 µs) | 1.6 µs | 2.9 µs | 2.9 µs |
| tiny-stable-stringify | 1.6 µs | 639,600 | ( 1.5 µs … 1.6 µs) | 1.6 µs | 1.6 µs | 1.6 µs |
summary
object-identity
2.73x faster than tiny-stable-stringify
2.97x faster than json-stable-stringify
11.71x faster than object-hash
^
object-identityonly handles 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