weakmap-oid

0.1.0 • Public • Published

weakmap-oid

Build Status

npm

Generate unique object IDs with WeakMap-based identity set.

Why do I need this?

weakmap-oid is useful when you need to do shallow (object identity) comparison across objects that were serialized (like in Electron's IPC module) or transformed to another language (like in CefSharp's JavaScript callbacks) or any other cases in which triple-equals (===) is no longer available. Sending a unique object ID along with the object eliminates the need for deep comparisons on the other side.

weakmap-oid uses WeakMap to maintain the mapping between objects and identities. Since native WeakMap does not prevent garbage collection of its key objects, using weakmap-oid will not lead to memory leak.

Installation

$ npm install weakmap-oid
 
# or 
 
$ yarn add weakmap-oid

Usage

import Oid from "weakmap-oid";
 
const oid = new Oid();
 
const a = {};
const b = {};
const c = b;
 
oid.id(a); // 0
oid.id(b); // 1
oid.id(c); // 1
 
oid.equal(a, b); // false
oid.equal(b, c); // true

Package Sidebar

Install

npm i weakmap-oid

Weekly Downloads

0

Version

0.1.0

License

MIT

Unpacked Size

5.26 kB

Total Files

6

Last publish

Collaborators

  • hyjk2000