mini-memoize
TypeScript icon, indicating that this package has built-in type declarations

1.0.5 • Public • Published

mini-memoize

A simple memoization library, The volume is very small.

Usage

import memoize from 'mini-memoize'

function add (a, b) {
  return a + b
}
const memoizeAdd = memoize(add)
memoizeAdd(1, 2) // excute
memoizeAdd(1, 2) // hit cache
memoizeAdd(1, 3) // excute
memoizeAdd(1, 3) // hit cache

promise

import memoize from 'mini-memoize'

function promise (a, b) {
  return new Promise(resolve => {
    setTimeout(() = {
      resolve(a + b)
    }, 3000)
  })
}
const memoizePromise = memoize(promise)
memoizePromise(1, 3).then(res => (console.log(res))) // excute
memoizePromise(1, 3).then(res => (console.log(res))) // hit cache
setTimeout(() = {
  memoizePromise(1, 3).then(res => (console.log(res))) // hit cache
}, 3000)

get

function add (a, b) {
  return a + b
}
const memoizeAdd = memoize(add)
memoizeAdd.get(1, 2) // undefined
memoizeAdd(1, 2) // 3
memoizeAdd.get(1, 2) // 3

has

function add (a, b) {
  return a + b
}
const memoizeAdd = memoize(add)
memoizeAdd(1, 2) // 3
memoizeAdd.has(1, 2) // true
memoizeAdd.has(1, 3) // false

delete

import memoize from 'mini-memoize'

function add (a, b) {
  return a + b
}
const memoizeAdd = memoize(add)
memoizeAdd(1, 2) // excute
memoizeAdd(1, 2) // hit cache
memoizeAdd.delete(1, 2) // delete
memoizeAdd(1, 2) // excute
memoizeAdd(1, 2) // hit cache
memoizeAdd.clear() // delete all
memoizeAdd(1, 2) // excute

max cache

import memoize from 'mini-memoize'

function add (a, b) {
  return a + b
}
const memoizeAdd = memoize(add, { max: 2 })
memoizeAdd(1, 2) // excute
memoizeAdd(1, 3)
memoizeAdd(1, 4)
memoizeAdd(1, 2) // excute

event

import memoize from 'mini-memoize'

function add (a, b) {
  return a + b
}
const memoizeAdd = memoize(add)

function handler (args) {
  console.log(args)
}

// on,type: get、delete、clear
memoizeAdd.on('delete', handler)

// off
memoizeAdd.off('delete', handler)

/mini-memoize/

    Package Sidebar

    Install

    npm i mini-memoize

    Weekly Downloads

    0

    Version

    1.0.5

    License

    MIT

    Unpacked Size

    11.9 kB

    Total Files

    5

    Last publish

    Collaborators

    • huangzhaoping