Skip to content

Class: WeakBiMap<K, V>

WeakBiMap is a bidirectional weak map that supports both primitive and object keys/values.

Features:

  • Supports both objects and primitives as keys and values
  • Automatic garbage collection for unreferenced objects
  • Bidirectional mapping (can look up by key or value)
  • Implements full Map interface
  • Memory efficient with automatic cleanup

Important notes about iteration:

  • Elements may be garbage collected during iteration
  • The iterator provides a snapshot at the time of iteration
  • No guarantees are made about elements that become unreachable during iteration

Example

typescript
const map = new WeakBiMap<object, string>()
const obj = { id: 1 }
map.set(obj, 'value')
console.log(map.get(obj)) // 'value'

// Objects are automatically garbage collected when no longer referenced

Type Parameters

K

K

The type of keys

V

V

The type of values

Implements

  • Map<K, V>

Constructors

Constructor

new WeakBiMap<K, V>(entries?): WeakBiMap<K, V>

Parameters

entries?

null | readonly readonly [K, V][]

Returns

WeakBiMap<K, V>

Accessors

[toStringTag]

Get Signature

get [toStringTag](): string

Returns

string

Implementation of

Map.[toStringTag]


size

Get Signature

get size(): number

Returns

number

the number of elements in the Map.

Implementation of

Map.size

Methods

[iterator]()

[iterator](): MapIterator<[K, V]>

Returns

MapIterator<[K, V]>

Implementation of

Map.[iterator]


clear()

clear(): void

Returns

void

Implementation of

Map.clear


delete()

delete(key): boolean

Parameters

key

K

Returns

boolean

true if an element in the Map existed and has been removed, or false if the element does not exist.

Implementation of

Map.delete


dispose()

dispose(): void

Dispose of the WeakBiMap and clean up resources

Returns

void


entries()

entries(): MapIterator<[K, V]>

Returns an iterable of key, value pairs for every entry in the map.

Returns

MapIterator<[K, V]>

Implementation of

Map.entries


forceCleanup()

forceCleanup(): void

Manually trigger cleanup of garbage collected entries. This is called automatically based on the cleanup strategy.

Returns

void


forEach()

forEach(callbackFunction, thisArgument?): void

Executes a provided function once per each key/value pair in the Map, in insertion order.

Parameters

callbackFunction

(value, key, map) => void

thisArgument?

any

Returns

void

Implementation of

Map.forEach


get()

get(key): undefined | V

Returns a specified element from the Map object. If the value that is associated to the provided key is an object, then you will get a reference to that object and any change made to that object will effectively modify it inside the Map.

Parameters

key

K

Returns

undefined | V

Returns the element associated with the specified key. If no element is associated with the specified key, undefined is returned.

Implementation of

Map.get


has()

has(key): boolean

Parameters

key

K

Returns

boolean

boolean indicating whether an element with the specified key exists or not.

Implementation of

Map.has


keys()

keys(): MapIterator<K>

Returns an iterable of keys in the map

Returns

MapIterator<K>

Implementation of

Map.keys


set()

set(key, value): this

Adds a new element with a specified key and value to the Map. If an element with the same key already exists, the element will be updated.

Parameters

key

K

value

V

Returns

this

Implementation of

Map.set


values()

values(): MapIterator<V>

Returns an iterable of values in the map

Returns

MapIterator<V>

Implementation of

Map.values

Released under the MIT License.