weakmap-memoize

0.0.3 • Public • Published

weakmap-memoize

This is a very lightweight WeakMap-based memoization wrapper that allows you to use your own preferred WeakMap implementation, of which there are many.

NOTE: You are constrained in that only the first argument to your function will used to do the memoization, and it must be an object.

Usage:

var memoize = require('weakmap-memoize')
var WeakMapShim = require('weakmap-shim')

var times = 0
var myFunction = memoize(WeakMapShim(), function (obj) {
    times++
    return obj.num + 1
})

var one = {num: 1}
var two = {num: 2}
assert.equal(myFunction(one), 2)
assert.equal(times, 1)
assert.equal(myFunction(one), 2)
assert.equal(times, 1)
assert.equal(myFunction(two), 3)
assert.equal(times, 2)
assert.equal(myFunction(two), 3)
assert.equal(times, 2)

myFunction = memoize(WeakMapShim(), function (obj1, obj2) {
    return obj1.num + obj2.num
})

assert.equal(myFunction(one, two), 3)

Readme

Keywords

Package Sidebar

Install

npm i weakmap-memoize

Weekly Downloads

3

Version

0.0.3

License

Public Domain

Last publish

Collaborators

  • danielnaab