An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

[![Build Status](https://travis-ci.org/lovasoa/memoization.svg?branch=master)](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;
```