rsms / js-lru

A fast, simple & universal Least Recently Used (LRU) map for JavaScript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Suggestion: custom size

osi-oswald opened this issue · comments

Did you consider the ability to provide an optional size parameter in set(key, value, size = 1)? For example:

c.set(key, usuallyBig, 100);
c.set(key, usuallySmall, 10);
c.set(key, foo, mySizeFn(foo));
c.set(key, bar); // <-- default size = 1

It could be stored in the Entry and can better approximate the "actual" size of all entries (especially the condition size > limit).

I don't think I follow — what would size mean in the context of a value?

Let's say you define a limit of 100 (100 of "what?"):

  • If you look at it as "limit of 100 values", then it makes totally sense to use size = 1 for each value.
  • If you look at it as "limit of 100 memory blocks", then some values might occupy more size than other values. It would even mean, that if I add a "big" value, it could result in purging a lot of "smaller" values to make enough room. (At least until the invariant size <= limit is true again).

So size in the context of a value would mean how much "it takes away" from the limit.
And size in the context of the LRU is just the sum of all values size.

I think that would overload the meaning and use-cases of a limited map like this. If you're storing variably-sized blocks, why not just store a handle to a block in the map? E.g. m.set('block-abc123', ['a', 'b', 'c']).

If you're looking to limit entries on a sum of some property of this, just replace the size property (you can define it as a dynamic property to—when called—check the sum of all value's lengths.) Or you can "subclass" the LRUMap object and override some of the functions that deal with insertion.