Giter Site home page Giter Site logo

weak-flyweight-set's Introduction

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.

⚠️ 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.

weak-flyweight-set's People

Contributors

lsndr avatar semantic-release-bot avatar

Stargazers

 avatar  avatar

Watchers

 avatar

weak-flyweight-set's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.