[ANN]: memoizee - Complete memoize/cache solution

353 views
Skip to first unread message

Mariusz Nowak

unread,
Sep 18, 2012, 6:06:21 PM9/18/12
to nod...@googlegroups.com
https://github.com/medikoo/memoize

npm install memoizee

For one of my projects I needed memoizer, that would be both fast and would work with any type of input arguments. Other projects demanded other common functionalities, so it's result of real world needs I approached in last months. To make it complete in latest stage I added max and maxAge options (lru-cache inspiration)

Features in short:
- Works with any type of function arguments – no serialization is needed
- Works with any length of function arguments. Length can be set as fixed or dynamic.
- One of the fastest available solutions (when talking about plain simple case it's slightly slower than basic underscore implementation and about 3 x faster than lru-cache)
- Support for asynchronous functions
- Optional primitive mode which assures fast performance when arguments are conversible to strings.
- Can be configured for methods (when `this` counts in)
- Cache can be cleared manually or after specified timeout
- Cache size can be limited
- Optionally accepts resolvers that normalize function arguments before passing them to underlying function.
- Optional reference counter mode, that allows more sophisticated cache management
- Profile tool that provides valuable usage statistics
- Covered by over 500 unit tests

I use it all over the place now. Let me know what you think, any feedback highly welcome

Jeff Barczewski

unread,
Sep 19, 2012, 11:14:57 AM9/19/12
to nod...@googlegroups.com
Mariusz,

memoize looks really nice! I am going to try it out for some projects I am working on.

As for suggestions:

 - you might consider wrapping the file in a function closure so it can be used directly in a browser
 - could you add a LRU option? (I know you have many other algorithms, but would be nice to have if it doesn't add too much bulk)

Thanks for sharing this!

All the best,

Jeff

Jeff Barczewski

unread,
Sep 19, 2012, 11:17:58 AM9/19/12
to nod...@googlegroups.com
Mariusz,

I took a closer look and realized that there a many dependencies (I was thinking it was self contained), so not as simple to get into the browser, either need to use something like AMD or build a distribution file with one of the commonsjs browser build solutions.

Jeff

Mariusz Nowak

unread,
Sep 19, 2012, 11:47:07 AM9/19/12
to nod...@googlegroups.com
Jeff,

It can easily be ported to browser with help of Webmake https://github.com/medikoo/modules-webmake (it's mentioned in documentation)
Anyway to make it even easier I prepared a bundle -> https://github.com/medikoo/memoize/downloads ;)

According to LRU option, you can achieve it with 'max' option -> https://github.com/medikoo/memoize#limiting-cache-size
See also 'maxAge' and 'dispose' options. Is that what you're after?

Jeff Barczewski

unread,
Sep 19, 2012, 3:36:38 PM9/19/12
to nod...@googlegroups.com
Mariusz,

You are right, I missed the mention in the readme about building it for the browser, but it is nice that you prepared a download bundle. Honestly I just went right to the code first and only glanced at the README for the API, so I ended up missing that.

Correct me if I am wrong, but isn't the max option, FIFO, so unlike a LRU cache which keeps the most recently used objects cached, this would expire the first one that was cached regardless of whether it was recently used/requested or not. 

One reason this is nice is that a LRU cache ends up staying loaded with the most popular content, the random requests don't end up kicking out an important one.

For many things, the max option would be fine, but just for completeness (and a very popular use case), it would be great to have a LRU option.

Just my thoughts for completing out the feature set. I think your project is great none the less.

Jeff 

Mariusz Nowak

unread,
Sep 21, 2012, 9:32:49 AM9/21/12
to nod...@googlegroups.com
Jeff, sorry for delayed response, I wanted to take some time and look into LRU case. Indeed it's much more useful approach than FIFO, I changed internal algorithm for max option to use that instead: https://github.com/medikoo/memoize/blob/master/lib/ext/max.js so now it's definitely backed by LRU.

I've also improved modularization, so other algorithms if needed can be easily configured. New browser bundle can be found in downloads section, if you want you can (using webmake) create bundle trimmed of functionalities you don't need - see lib/index.js how it's done, one that I've uploaded provides everything.

Mariusz

Vitaly Puzrin

unread,
Sep 23, 2012, 6:45:48 PM9/23/12
to nod...@googlegroups.com
Mariusz,

Thanks for sharing memoizee.

I have comment about timeouts. Very often, it's recommended to use "replace" strategy, instead of "delete". I mean,
if data is expired, you still continue to return old data from cache, but fetch new one in background. It can be easily implemented
in async mode. That has 2 benefits:
- no freezes on expires
- easy to avoid parallel refresh requests

Vitaly.

среда, 19 сентября 2012 г., 2:06:21 UTC+4 пользователь Mariusz Nowak написал:

Mariusz Nowak

unread,
Sep 24, 2012, 7:37:12 AM9/24/12
to nod...@googlegroups.com
Vitaly,

Most cases I've approached with memoize was to provide value *on demand*, so not to generate additional work if there's no need to. If it expires it's vanished, maybe it won't be needed again, leave it the way it is.
Anyway I can imagine cases where behavior you describe can be desirable, so it`s probably worthwhile extension (and quite easy to add), I've added issue https://github.com/medikoo/memoize/issues/1, so it'll soon be possible. Thanks! :)

Jeff Barczewski

unread,
Oct 1, 2012, 1:32:40 PM10/1/12
to nod...@googlegroups.com
Excellent! Thanks for all your work on this!
Reply all
Reply to author
Forward
0 new messages