Skip to content

Generate unique object IDs with WeakMap-based identity set.

License

Notifications You must be signed in to change notification settings

hyjk2000/weakmap-oid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Generate unique object IDs with WeakMap-based identity set.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published