1.2.0 • Published 7 years ago

triemoize v1.2.0

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

Triemoize

Efficient, reliable and memory leak free memoization using WeakMaps and tries

build status npm version License: MIT

There are many memoization libraries out there. What this library tries to achieve is to be

  • Efficient
  • Reliable
  • Memory leak free

For all this to work, you must always use immutable data as function arguments when you're calling the memoized function.

Installation

npm install --save triemoize

If you're using Yarn

yarn add triemoize

Usage

import memoize from 'triemoize'

// Create a pure function
const add = (a, b) = a + b

// Memoize it
const memoizedAdd = memoize(add)

memoizedAdd(1, 2) // The function was called
memoizedAdd(1, 2) // This time the result was taken from cache

Unlike from other memoization techniques like serialization, you can safely use huge objects or arrays as function arguments without worrying about performance or memory leaks. However, all the arguments you pass in must be immutable. Otherwise the memoization would not work correctly.

const getStringsLessThan = memoize((arrayOfStrings, num) => {
  return arrayOfStrings.filter(item => item.length < num)
})

const food = [
  'milk', 'bread', 'cheese', 'chocolate', 'potatoes', 'bacon',
  'butter', 'eggs', 'sausages', 'pork', 'yoghurt', 'apple'
]

getStringsLessThan(food, 6) // The function was called
getStringsLessThan(food, 6) // The result was taken from cache

Named arguments

Named arguments (using ES6 destructuring) are not supported in the moment. I plan to add it later.

// Not supported in the moment

const add = ({ a, b }) = a + b
const memoizedAdd = memoize(add)

memoizedAdd({ a: 1, b: 2}) // The function was called
memoizedAdd({ a: 1, b: 2}) // Fail, the value was recomputed

How it works

TODO

Benchmark

TODO

License

MIT

1.2.0

7 years ago

1.1.0

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago