"Fossies" - the Fresh Open Source Software Archive

Member "node-v12.18.4-win-x64/node_modules/npm/node_modules/mem/readme.md" (18 Feb 2020, 3511 Bytes) of package /windows/www/node-v12.18.4-win-x64.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format (assuming markdown format). Alternatively you can here view or download the uninterpreted source code file. A member file download can also be achieved by clicking within a package contents listing on the according byte size field.

mem Build Status

Memoize functions - An optimization used to speed up consecutive function calls by caching the result of calls with identical input

Memory is automatically released when an item expires.

Install

$ npm install mem

Usage

const mem = require('mem');

let i = 0;
const counter = () => ++i;
const memoized = mem(counter);

memoized('foo');
//=> 1

// Cached as it's the same arguments
memoized('foo');
//=> 1

// Not cached anymore as the arguments changed
memoized('bar');
//=> 2

memoized('bar');
//=> 2
Works fine with promise returning functions
const mem = require('mem');

let i = 0;
const counter = async () => ++i;
const memoized = mem(counter);

(async () => {
    console.log(await memoized());
    //=> 1

    // The return value didn't increase as it's cached
    console.log(await memoized());
    //=> 1
})();
const mem = require('mem');
const got = require('got');
const delay = require('delay');

const memGot = mem(got, {maxAge: 1000});

(async () => {
    await memGot('sindresorhus.com');

    // This call is cached
    await memGot('sindresorhus.com');

    await delay(2000);

    // This call is not cached as the cache has expired
    await memGot('sindresorhus.com');
})();

API

mem(fn, options)

fn

Type: Function

Function to be memoized.

options

Type: Object

maxAge

Type: number
Default: Infinity

Milliseconds until the cache expires.

cacheKey

Type: Function

Determines the cache key for storing the result based on the function arguments. By default, if there’s only one argument and it’s a primitive, it’s used directly as a key, otherwise it’s all the function arguments JSON stringified as an array.

You could for example change it to only cache on the first argument x => JSON.stringify(x).

cache

Type: Object
Default: new Map()

Use a different cache storage. Must implement the following methods: .has(key), .get(key), .set(key, value), .delete(key), and optionally .clear(). You could for example use a WeakMap instead or quick-lru for a LRU cache.

cachePromiseRejection

Type: boolean
Default: false

Cache rejected promises.

mem.clear(fn)

Clear all cached data of a memoized function.

fn

Type: Function

Memoized function.

Tips

Cache statistics

If you want to know how many times your cache had a hit or a miss, you can make use of stats-map as a replacement for the default cache.

Example

const mem = require('mem');
const StatsMap = require('stats-map');
const got = require('got');

const cache = new StatsMap();
const memGot = mem(got, {cache});

(async () => {
    await memGot('sindresorhus.com');
    await memGot('sindresorhus.com');
    await memGot('sindresorhus.com');

    console.log(cache.stats);
    //=> {hits: 2, misses: 1}
})();

License

MIT © Sindre Sorhus