@pacote/memoize
TypeScript icon, indicating that this package has built-in type declarations

2.1.1 • Public • Published

@pacote/memoize

version minified minified + gzip

Speed up consecutive calls of the same function with the same arguments by caching its results.

Installation

yarn add @pacote/memoize

Usage

import { memoize } from '@pacote/memoize'

const randomFunction = (prefix: string) => `${prefix}${Math.random()}`

const memoizedFunction = memoize((prefix) => `key_${prefix}`, randomFunction)

memoizedFunction('foo') // 'foo' followed by randomly-generated number.
memoizedFunction('foo') // Same result as previous call with 'foo'.

memoizedFunction('bar') // 'bar' followed by randomly-generated number.
memoizedFunction('bar') // Same result as previous call with 'bar'.

memoize<A, T>(cacheKeyFn: (...args: A) => string, fn: (...args: A) => T): (...args: A) => T

memoize() takes two function arguments:

  • A function that generates a string key for cached results. This function takes the same arguments as the function to memoize.

  • The function to memoize. memoize() returns a version of this function that caches results.

License

MIT © Luís Rodrigues.

Package Sidebar

Install

npm i @pacote/memoize

Weekly Downloads

746

Version

2.1.1

License

MIT

Unpacked Size

11.6 kB

Total Files

13

Last publish

Collaborators

  • goblindegook