@joepie91/objid
v1.0.1
Published
Small debugging utility that assigns unique IDs to objects without polluting them
Downloads
1
Maintainers
Readme
@joepie91/objid
Tiny development utility. Assigns a unique ID to every object you give it, that persists for the lifetime of that object. Requires WeakMap support (to ensure it doesn't cause memory leaks by preventing garbage collection).
Particularly useful for eg. logging debug messages that may relate to any number of instances of the same kind of object, and you don't want to pass around a bunch of extra arguments to keep track of which one came from where. Instead you can correlate by logging the IDs of the instances, without having to pass any data anywhere other than the value itself.
Example
"use strict";
const objid = require("@joepie91/objid");
let a = {};
let b = {};
let c = new Date();
let d = function() {};
console.log(objid(a)); // 0
console.log(objid(b)); // 1
console.log(objid(c)); // 2
console.log(objid(d)); // 3
console.log(objid(b)); // 1 (an ID was assigned to that object previously)