1.0.4 • Published 2 years ago
weak-ref-cache v1.0.4
Weak-Ref-Cache
The weak-ref-cache package is a utility module designed to help you manage and reuse weak references to objects in JavaScript. With this package, you can retrieve weak references to objects and compare them using the same equality comparison as you would use for the objects themselves. Furthermore, it ensures that only one WeakRef instance is created per object, promoting memory efficiency.
Installation
npm install weak-ref-cacheUsage
Here's how you can use the weak-ref-cache package:
- Import the
getWeakReffunction from the package. - Use the
getWeakReffunction to get a weak reference to an object. - You can compare weak references obtained using the
getWeakReffunction just like you would compare the objects themselves.
Example
import getWeakRef from "weak-ref-cache";
const obj = { foo: "bar" };
const weakRef = getWeakRef(obj);
console.log(weakRef === getWeakRef(obj)); // true
console.log(weakRef === new WeakRef(obj)); // falseAPI
getWeakRef(obj: object): WeakRef
- Parameters:
obj(Object): The object for which you want to get a weak reference.
- Returns:
- A
WeakRefinstance representing a weak reference to theobj. If a weak reference to theobjwas previously created bygetWeakRef, it returns the sameWeakRefinstance.
- A