Skip to content

pMemoizeDecorator

pMemoizeDecorator(options?): (target: any, propertyKey: any, descriptor: any) => void

  • Only class methods and getters/setters can be memoized, not regular functions (they aren't part of the proposal);
  • Only TypeScript’s decorators are supported, not Babel’s, which use a different version of the proposal;
  • Being an experimental feature, they need to be enabled with --experimentalDecorators; follow TypeScript’s docs.

参数

NameType
optionsObject

返回值

fn

A decorator to memoize class methods or static class methods.

▸ (target, propertyKey, descriptor): void

参数
NameType
targetany
propertyKeyany
descriptorany
返回值

void

示例

import {pMemoizeDecorator} from 'p-memoize';

class Example {
    index = 0

    @pMemoizeDecorator()
    async counter() {
        return ++this.index;
    }
}

class ExampleWithOptions {
    index = 0

    @pMemoizeDecorator()
    async counter() {
        return ++this.index;
    }
}

源码

memoize.js