memozie

1.3.5 • Public • Published

memozie

Dead simple customizable memoization in javascript.

Installation

run npm install memozie

Usage

You can use any number of arguments on functions. Simply wrap the function in memoize function (shown below).

import { memoize, memoizeAll } from "memozie"
const addition = memoize((a,b) => {
  console.log("inside addition function")
  return a + b
})

console.log(addition(4,5)) // outputs "inside addition function" and "9"
addition(4,5) // outputs "9" pulls in value from cached fn results
addition(4,5) // outputs "9" pulls in value from cached fn results

// DEMO WITH ARBITRARY NUMBER OF ARGUMENTS
const mergeObjects = memoize((...objects) => {
  console.log("inside merge objects function")
  return objects.reduce((acc, obj) => Object.assign(acc, obj) , {});
})

console.log(mergeObjects({v: 'v'}, {c : 'c'}, {n: 'n'})) // outputs object and "inside merge objects function"
console.log(mergeObjects({v: 'v'}, {c : 'c'}, {n: 'n'})) // just outputs the object
console.log(mergeObjects({d: 'd'}, {c : 'c'}, {n: 'n'})) // outputs object and "inside merge objects function"


// Can memoize multiple functions and once with memoizeAll

memoizeAll([(a, b) => a-b, (v, c, d) => v - c - d])

Configuration

Second parameter is options which does partial overrides of the defaultOptions (ie you only need to override what you need).

  • getKey(args): Function which generates a key from the arguments to cache value.
  • checkCache(cache, key): Function which checks the cache with the given key to see if value present.
  • run(func, args): Runs passed in function with the given arguments (only occurs when not found in cache).

Examples

// OVERRIDE OF getKey
const additionCustomKey = memoize(
  (a,b) => {console.log("inside addition function"); return a + b;}, 
  {getKey: (args) => args[0] + args[1]}
)

console.log(additionCustomKey(4,5)) // outputs "inside addition function" and "9"
console.log(additionCustomKey(4,5)) // outputs just "9"
console.log(additionCustomKey(3,6)) // outputs just "9" because of custom getKey function

// OVERRIDE OF checkCache
const additionCustomCache = memoize(
  (a,b) => {console.log("inside addition function"); return a + b;}, 
  {checkCache: (cache, key) => key === 0 && key in cache}
)

console.log(additionCustomCache(4,5)) // outputs "inside addition function" and "9"
console.log(additionCustomCache(4,5)) // outputs "inside addition function" and "9" because override of check cache only works for key of zero

// OVERRIDE OF run
const additionCustomRun = memoize(
  (a,b) => {console.log("inside addition function"); return a + b;}, 
  {run: (fn, args) => {console.log("hey"); return fn(...args) }}
)

console.log(additionCustomRun(4,5)) //outputs "hey", "inside addition function" and "9" because of run override
console.log(additionCustomRun(4,5)) //outputs just "9" because hitting cache

Readme

Keywords

none

Package Sidebar

Install

npm i memozie

Weekly Downloads

0

Version

1.3.5

License

MIT

Last publish

Collaborators

  • chesslunatic