Socket
Book a DemoInstallSign in
Socket

weak-flyweight-set

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

weak-flyweight-set

The `WeakFlyweightSet` object holds weak references to similar objects.

next
latest
Source
npmnpm
Version
1.1.1
Version published
Maintainers
1
Created
Source

WeakFlyweightSet

The WeakFlyweightSet object holds weak references to similar objects.

A weak reference to an object is a reference that does not prevent the object from being reclaimed by the garbage collector. In contrast, a normal (or strong) reference keeps an object in memory.

Flyweight is a structural design pattern that lets you fit more objects into the available amount of RAM by sharing common parts of state between multiple objects instead of keeping all of the data in each object.

Refactoring Guru

The WeakFlyweightSet is based on Set, WeakRef and FinalizationRegistry objects.

  • No dependencies
  • TypeScript
  • ES2021
  • Node.js >=14.6.0

Installation

npm i weak-flyweight-set

Example

const set = new WeakFlyweightSet();

let valueObject = {key1: 'value: 1', key2: 'value: 2'};

set.add(valueObject);

// all of the following calls return a reference to the same object
set.get({key1: 'value: 1', key2: 'value: 2'});
set.use({key1: 'value: 1', key2: 'value: 2'});

valueObject = null;

// ...
// GC reclaims space
// ...

// returns undefined
set.get({key1: 'value: 1', key2: 'value: 2'});

// return a reference to a new object
set.use({key1: 'value: 1', key2: 'value: 2'});

API

size: number

Returns the number of values in the WeakFlyweightSet object.

:warning: Number of values is not precise due to nature of FinalizationRegistry object.

add(object): this

The add() method adds an object to a WeakFlyweightSet object.

get(object): object | undefined

The get() method returns a reference to a similar object from a WeakFlyweightSet object or undefined.

use(object): object

The use() method returns a reference to a similar object from a WeakFlyweightSet object or adds the specified object to the set and returns a reference to it.

has(object): boolean

The has() method returns a boolean indicating whether a similar object exists or not.

delete(object): boolean

The delete() method removes a similar object from a WeakFlyweightSet.

values(): IterableIterator<object>

The values() method returns a new Iterator object that contains the values for each element in the WeakFlyweightSet object in insertion order.

forEach(cb): void

The forEach() method executes a provided function once per each value in the WeakFlyweightSet object, in insertion order.

clear(): void

The clear() method removes all elements from a WeakFlyweightSet object.

License

WeakFlyweightSet is MIT licensed.

FAQs

Package last updated on 11 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts