erikras / lru-memoize

A utility to provide LRU memoization for any js function
MIT License
316 stars 20 forks source link

"move to top" behavior uses slice, should use splice #87

Open aduth opened 6 years ago

aduth commented 6 years ago

At this line, I expect you are intending to use Array#splice, not Array#slice:

https://github.com/erikras/lru-memoize/blob/86f655e17032e055187f4bb492e0c6937fa48318/src/lruCache.js#L13

Splice is mutative and as used would result in the item being removed at the given index. Slice is non-mutative and as used does nothing (aside from wasting cycles to create a copy of the array starting at index 1), therefore the cache entry will exist twice in the entries set (since it is not removed, then later prepended to the set).

jimmytuc commented 6 years ago

Related #88