Skip to content

memo

Memoize a function

307 bytes

Usage

Wrap a function with memo to get a function back that automagically returns values that have already been calculated.

import * as
import _
_
from 'radashi'
const
const timestamp: () => number
timestamp
=
import _
_
.
memo<[], number>(func: () => number, options?: _.MemoOptions<[]> | undefined): () => number
export memo

Creates a memoized function. The returned function will only execute the source function when no value has previously been computed. If a ttl (milliseconds) is given previously computed values will be checked for expiration before being returned.

@seehttps://radashi.js.org/reference/curry/memo

@example

const calls: number[] = []
const fib = memo((x: number) => {
calls.push(x)
return x < 2 ? x : fib(x - 1) + fib(x - 2)
})
fib(10) // 55
fib(10) // 55
// calls === [10]
fib(11) // 89
// calls === [10, 11]

@version12.1.0

memo
(() =>
var Date: DateConstructor

Enables basic storage and retrieval of dates and times.

Date
.
DateConstructor.now(): number

Returns the number of milliseconds elapsed since midnight, January 1, 1970 Universal Coordinated Time (UTC).

now
())
const
const now: number
now
=
const timestamp: () => number
timestamp
()
const
const later: number
later
=
const timestamp: () => number
timestamp
()
const now: number
now
===
const later: number
later
// => true

Expiration

You can optionally pass a ttl (time to live) that will expire memoized results. In versions prior to version 10, ttl had a value of 300 milliseconds if not specified.

import * as
import _
_
from 'radashi'
const
const timestamp: () => number
timestamp
=
import _
_
.
memo<[], number>(func: () => number, options?: _.MemoOptions<[]> | undefined): () => number
export memo

Creates a memoized function. The returned function will only execute the source function when no value has previously been computed. If a ttl (milliseconds) is given previously computed values will be checked for expiration before being returned.

@seehttps://radashi.js.org/reference/curry/memo

@example

const calls: number[] = []
const fib = memo((x: number) => {
calls.push(x)
return x < 2 ? x : fib(x - 1) + fib(x - 2)
})
fib(10) // 55
fib(10) // 55
// calls === [10]
fib(11) // 89
// calls === [10, 11]

@version12.1.0

memo
(() =>
var Date: DateConstructor

Enables basic storage and retrieval of dates and times.

Date
.
DateConstructor.now(): number

Returns the number of milliseconds elapsed since midnight, January 1, 1970 Universal Coordinated Time (UTC).

now
(), {
MemoOptions<TArgs extends any[]>.ttl?: number
ttl
: 1000, // milliseconds
})
const
const now: number
now
=
const timestamp: () => number
timestamp
()
const
const later: number
later
=
const timestamp: () => number
timestamp
()
await
import _
_
.
function sleep(milliseconds: number): Promise<void>
export sleep

Create a promise that resolves after a given amount of time.

@seehttps://radashi.js.org/reference/async/sleep

@example

await sleep(1000)

@version12.1.0

sleep
(2000)
const
const muchLater: number
muchLater
=
const timestamp: () => number
timestamp
()
const now: number
now
===
const later: number
later
// => true
const now: number
now
===
const muchLater: number
muchLater
// => false

Key Function

You can optionally customize how values are stored when memoized.

const
const timestamp: (args_0: {
group: string;
}) => string
timestamp
=
import _
_
.
memo<[{
group: string;
}], string>(func: (args_0: {
group: string;
}) => string, options?: _.MemoOptions<[{
group: string;
}]> | undefined): (args_0: {
group: string;
}) => string
export memo

Creates a memoized function. The returned function will only execute the source function when no value has previously been computed. If a ttl (milliseconds) is given previously computed values will be checked for expiration before being returned.

@seehttps://radashi.js.org/reference/curry/memo

@example

const calls: number[] = []
const fib = memo((x: number) => {
calls.push(x)
return x < 2 ? x : fib(x - 1) + fib(x - 2)
})
fib(10) // 55
fib(10) // 55
// calls === [10]
fib(11) // 89
// calls === [10, 11]

@version12.1.0

memo
(
({
group: string
group
}: {
group: string
group
: string }) => {
const
const ts: number
ts
=
var Date: DateConstructor

Enables basic storage and retrieval of dates and times.

Date
.
DateConstructor.now(): number

Returns the number of milliseconds elapsed since midnight, January 1, 1970 Universal Coordinated Time (UTC).

now
()
return `${
const ts: number
ts
}::${
group: string
group
}`
},
{
MemoOptions<[{ group: string; }]>.key?: (args_0: {
group: string;
}) => string
key
: ({
group: string
group
}: {
group: string
group
: string }) =>
group: string
group
,
},
)
const
const now: string
now
=
const timestamp: (args_0: {
group: string;
}) => string
timestamp
({
group: string
group
: 'alpha' })
const
const later: string
later
=
const timestamp: (args_0: {
group: string;
}) => string
timestamp
({
group: string
group
: 'alpha' })
const
const beta: string
beta
=
const timestamp: (args_0: {
group: string;
}) => string
timestamp
({
group: string
group
: 'beta' })
const now: string
now
===
const later: string
later
// => true
const beta: string
beta
===
const now: string
now
// => false