https://github.com/danielnaab/weakmap-memoize
Lightweight WeakMap-based function memoization.
https://github.com/danielnaab/weakmap-memoize
Last synced: 3 months ago
JSON representation
Lightweight WeakMap-based function memoization.
- Host: GitHub
- URL: https://github.com/danielnaab/weakmap-memoize
- Owner: danielnaab
- Created: 2015-12-21T22:54:27.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-12-22T00:32:53.000Z (over 10 years ago)
- Last Synced: 2025-12-29T04:18:31.590Z (7 months ago)
- Language: JavaScript
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# weakmap-memoize
This is a very lightweight [`WeakMap`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap)-based memoization wrapper that allows you to
use your own preferred `WeakMap` implementation, of which there are
[many](https://www.npmjs.com/search?q=weakmap).
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)