async-memo-ize

0.2.0 • Public • Published

Async Memo-ize

npm version CircleCI Greenkeeper badge

In computing, memoization or memoisation is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again — Wikipedia

This library makes async function, aka Promises, first class citizen with memoization

Use cases covered:

  1. An expensive function call (eg. API calls, intensive CPU calculations, etc)
  2. Multiple nodejs instances with a centralized cache (eg. Redis)

Notice: sync function can be used too

Real project use case

A NodeJS cluster computes a calculation every day for each user. The calculation is incremental using the data from the last 90 days. With this approach, the calculus can be distributed across all the available nodes, and the results are shared among them via a distributed cache (e.g. Redis) So that, it isn't necessary crunching data from the previous days again and again.

Install

npm install async-memo-ize

or

yarn add  async-memo-ize

Usage

Named functions

import memoize from 'async-memo-ize'
import sleep from 'sleep-promise';
 
const whatsTheAnswerToLifeTheUniverseAndEverything = async () => {
     await sleep(2000);
     return 42
}
const memoized = memoize(whatsTheAnswerToLifeTheUniverseAndEverything)
 
const answer = await memoized() // wait 2 seconds 
const quickAnswer = await memoized() // wait ms  

Anonymous functions

import memoize from 'async-memo-ize'
import sleep from 'sleep-promise';
 
const whatsTheAnswerToLifeTheUniverseAndEverything = memoize(async () => {
                                                                  await sleep(2000);
                                                                  return 42
                                                             }, {id: 'whatsTheAnswerToLifeTheUniverseAndEverything'})
 
const answer = await whatsTheAnswerToLifeTheUniverseAndEverything() // wait 2 seconds 
const quickAnswer = await whatsTheAnswerToLifeTheUniverseAndEverything() // wait ms  

If you prefer to memoize anonymous function, you have to pass a unique id. The id is used to generate the cache key and it is required to share the same cache across multiple memoized functions. Named functions don't need because the lib rely on fn.name as id

Cache

In Memory

A simple in memory async cache based on native js Map is provided.

Usage

import memoize, {LocalCache} from 'async-memo-ize'
 
const fn = async () => Promise.resolve(42)
const memoized = memoize(fn, new LocalCache)
 
const answer = await memoized() // wait ms  

You can provide your own implementation given the below interface:

class LocalCache {

  async has(key) {
    ...
  }

  async get(key) {
    ...
  }

  async set(key, value) {
    ...
  }

  async del(key) {
    ...
  }

  async entries() {
    ...
  }

  async size() {
    ...
  }
}

Plugins

  • RedisCache Distributed cache across functions and NodeJs instances

Package Sidebar

Install

npm i async-memo-ize

Weekly Downloads

339

Version

0.2.0

License

MIT

Last publish

Collaborators

  • aboutlo