https://github.com/lovasoa/memoization
Straightforward implementation of memoization in javascript
https://github.com/lovasoa/memoization
Last synced: 10 months ago
JSON representation
Straightforward implementation of memoization in javascript
- Host: GitHub
- URL: https://github.com/lovasoa/memoization
- Owner: lovasoa
- License: bsd-2-clause
- Created: 2015-12-15T22:24:55.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2016-11-02T14:15:39.000Z (about 9 years ago)
- Last Synced: 2025-03-19T02:00:01.720Z (10 months ago)
- Language: JavaScript
- Size: 5.86 KB
- Stars: 6
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/lovasoa/memoization)
# memoization
Straightforward implementation of memoization in javascript
## Exemple Usage
```js
var memoize = require("memoization");
var fib = memoize(function(n){return n<2 ? n : fib(n-1) + fib(n-2);});
```
## Code
```js
function memoize(f) {
var dict = {};
return function() {
var args = JSON.stringify(arguments);
if(dict.hasOwnProperty(args)) return dict[args];
var res = f.apply(this, arguments);
dict[args] = res;
return res;
}
}
if (typeof module === "object") module.exports = memoize;
```