Skip to content

memo()

ts
function memo<T>(validator): T

Memoizes a validator function—a parser or a guard. Internally, a memoized function uses a WeakMap to prevent memory leaks. While you can wrap any validator function within memo, primitive values will not be memoized.

Type Parameters

Type Parameter
T extends (arg) => unknown

Parameters

ParameterTypeDescription
validatorTA parser or guard function.

Returns

T

A memoized version of validator.

Examples

ts
const parseUser = memo(object({
 id: parseNumber,
 name: parseString,
}))

It can be used to for nested properties

ts
const parseUser = object({
  id: parseNumber,
  name: parseString,
  address: memo(object({
    street: parseString,
    city: parseString,
  })),
})

memoizeValidatorConstructor()

ts
function memoizeValidatorConstructor<T>(validatorConstructor): T

Given a higher order function that constructs a validator, returns a new function that also constructs a validator, but the validator will be memoized.

Example

ts
const objectMemo = memo2(object)
const parseUser = objectMemo({
 id: parseNumber,
 name: parseString,
})

Type Parameters

Type Parameter
T extends (...schema) => Parser<unknown> | Guard<unknown>

Parameters

ParameterTypeDescription
validatorConstructorTA higher order function that takes a schema and constructs a validator—a parser or a guard.

Returns

T

A function of the same type of validatorConstructor, but when called, the returned function is memoized.