You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

919 B

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("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)