/mem

Tiny, modern memoization library for unary functions

Primary LanguageJavaScript

mem

Tiny, modern memoization library for unary functions

Up and running

Get the package right from GitHub repository:

npm install --save shvaikalesh/mem

Use default export:

import mem from "mem"

Caching

  • Objects: identity-based, weakly-referenced
  • Primitives: SameValueZero algorithm

Example

Utilizing template caching (see [[TemplateMap]]) to speed up tagged literals:

const dropSpaces = mem(raw => {
  return raw.map(str => str.replace(/\s+/g, ""))
})

export default ({raw}, ...vals) => {
  return String.raw({raw: dropSpaces(raw)}, ...vals)
}