Skip to content

getOrInsertComputed

Returns a map entry or stores a computed value when missing

110 bytes
since v12.7.0

Usage

Computes and stores a value only when the key has no entry yet.

import * as
import _
_
from 'radashi'
const
const counts: Map<string, number>
counts
= new
var Map: MapConstructor
new <string, number>(iterable?: Iterable<readonly [string, number]> | null | undefined) => Map<string, number> (+3 overloads)
Map
<string, number>()
import _
_
.
function getOrInsertComputed<string, number>(map: Map<string, number>, key: string, factory: () => number): number (+1 overload)

Returns a map entry or stores the computed value when the key is missing.

@seehttps://radashi.js.org/reference/object/getOrInsertComputed

@example

const counts = new Map<string, number>()
getOrInsertComputed(counts, 'clicks', () => 1)
getOrInsertComputed(counts, 'clicks', () => 5)
// => 1

@version12.7.0

getOrInsertComputed
(
const counts: Map<string, number>
counts
, 'clicks', () => 1) // => 1
import _
_
.
function getOrInsertComputed<string, number>(map: Map<string, number>, key: string, factory: () => number): number (+1 overload)

Returns a map entry or stores the computed value when the key is missing.

@seehttps://radashi.js.org/reference/object/getOrInsertComputed

@example

const counts = new Map<string, number>()
getOrInsertComputed(counts, 'clicks', () => 1)
getOrInsertComputed(counts, 'clicks', () => 5)
// => 1

@version12.7.0

getOrInsertComputed
(
const counts: Map<string, number>
counts
, 'clicks', () => 5) // => 1

This function is most useful when the computed value is expensive to compute, e.g. a complex object or a non-trivial math calculation.

You can use a WeakMap instead, if you’d like to associate the computed value with an object reference.

import * as
import _
_
from 'radashi'
const
const values: WeakMap<object, {
count: number;
}>
values
= new
var WeakMap: WeakMapConstructor
new <object, {
count: number;
}>(entries?: readonly (readonly [object, {
count: number;
}])[] | null | undefined) => WeakMap<object, {
count: number;
}> (+1 overload)
WeakMap
<object, {
count: number
count
: number }>()
const
const key: {}
key
= {}
import _
_
.
function getOrInsertComputed<{}, {
count: number;
}>(map: Map<{}, {
count: number;
}> | WeakMap<{}, {
count: number;
}>, key: {}, compute: () => {
count: number;
}): {
count: number;
} (+1 overload)

Returns a map entry or stores the computed value when the key is missing.

@seehttps://radashi.js.org/reference/object/getOrInsertComputed

@example

const counts = new Map<string, number>()
getOrInsertComputed(counts, 'clicks', () => 1)
getOrInsertComputed(counts, 'clicks', () => 5)
// => 1

@version12.7.0

getOrInsertComputed
(
const values: WeakMap<object, {
count: number;
}>
values
,
const key: {}
key
, () => ({
count: number
count
: 1 })) // => { count: 1 }
import _
_
.
function getOrInsertComputed<{}, {
count: number;
}>(map: Map<{}, {
count: number;
}> | WeakMap<{}, {
count: number;
}>, key: {}, compute: () => {
count: number;
}): {
count: number;
} (+1 overload)

Returns a map entry or stores the computed value when the key is missing.

@seehttps://radashi.js.org/reference/object/getOrInsertComputed

@example

const counts = new Map<string, number>()
getOrInsertComputed(counts, 'clicks', () => 1)
getOrInsertComputed(counts, 'clicks', () => 5)
// => 1

@version12.7.0

getOrInsertComputed
(
const values: WeakMap<object, {
count: number;
}>
values
,
const key: {}
key
, () => ({
count: number
count
: 2 })) // => { count: 1 }

Inspired by TC39’s Upsert proposal.