-
Notifications
You must be signed in to change notification settings - Fork 672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added a simple FIFO cache to defaultMemoize, with a cacheSize option #238
Conversation
Codecov Report
@@ Coverage Diff @@
## master #238 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 1 1
Lines 15 24 +9
=====================================
+ Hits 15 24 +9
Continue to review full report at Codecov.
|
93f66e5
to
99645fb
Compare
3 similar comments
99645fb
to
8837c03
Compare
8837c03
to
e0860b2
Compare
e0860b2
to
efe765c
Compare
3 similar comments
f1417fd
to
9babf91
Compare
b074b0f
to
6e97916
Compare
6e97916
to
eae325d
Compare
1 similar comment
…l defaultMemoize when cacheSize === 1.
eae325d
to
6721a43
Compare
I made some changes recently: I added a After running the performance benchmarks many times, I concluded that the new |
Also it would be great if coveralls could only comment when there's a change |
cc: @ellbee I was wondering if someone might be able to take a look at this? I've been using this branch in my own app for the last few weeks, and it seems to be working great for me. |
@ndbroadbent |
Hi @b6pzeusbc54tvhw5jgpyw8pwz2x6gs - I actually use |
@ellbee any possibility of an update on this? I have a use case where I need a cache size of exactly two. This would be extremely handy. Thanks! |
Ping 😃. From version 1.0.0,
Greetings! |
Superseded by #513 . |
I was using
memoize
fromlodash
to create some unbounded caches, which is fine for certain parts of my code. I also know aboutre-reselect
, but that solves a slightly different set of problems.There are a few cases in my app where I really, really want to have a specific cache size, so that it would store the last 2 or 3 results. One example is where I am using selectors for both old and new props, and comparing the results. Another example is where I have a boolean prop that frequently switches between true and false.
So I've added a
cacheSize
option todefaultMemoize
. IfcacheSize
is one, then we fall back to the originaldefaultMemoize
code (renamed tomemoizeOne
.)If
cacheSize
is more than one, then we store the args and results in an array. We start appending results to the end of the array and move backwards, until it wraps around to the end and starts overwriting the oldest result. It uses alastIndex
pointer to keep track of the most recent results, so the search always runs from newest to oldest.I added benchmark tests, and they look fine on my machine. Here's the output after I added a temporary for loop to run them each 5 times:
P.S. I should also mention that I took some inspiration (and tests) from #210. They implemented a LRU cache, but I don't think the performance hit is worth it, and I don't think it was implemented in a very performant way.