-
Notifications
You must be signed in to change notification settings - Fork 5
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
make Map the default (round 2) #40
Comments
@gurgunday That's an interesting finding, but also the one that needs to be documented! Sometimes integers will be used as a keys, as they are a popular choice for primary ids in the databases; but sometimes uuids or some other alphanumeric options are used; so in ideal scenario we should have benchmarks for both scenarios, and provide recommendations about what to use in the project readme. Oh, and fastify-rate-limit can switch to map-based cache already, both are exposed. Could you send a PR to https://github.com/kibertoad/nodejs-benchmark-tournament, adding separate benchmarks for string-based ids for inmemory caches? Based on that I could adjust the default and the readme. |
Will do that! As you said, using the map import explicitly for rate-limit would be smarter as we can provide a solid reasoning for it if someone asks, will send the PR there after confirming results with you |
I finally cracked the code after digging through more benchmarks and stumbling upon the comment of a V8 developper
TLDR is that if we use ordered keys as we do in the benchmarks, V8 will highly optimize property access. To illustrate, if we just force the keys to be non-numeric, with something like
x + '_'
, the picture becomes much more interesting :)Benchmarks
Let's modify the execute function to make our keys nonnumeric, the following results surface for
cache-get-inmemory/
:Here's the execute functions of
toad-cache-lru-map.js
andtoad-cache-lru-object.js
:Then, I realized another potential issue with the default parameters —
MAX_ITEMS
was higher thanELEMENT_COUNT
Ideally,
MAX_ITEMS
should be less thanELEMENT_COUNT
so that we can have deletions as well to get a more complete picture of performanceLet's modify
common.js
:Here are the results of a rerun:
I argue that a generic LRU will not have number-based keys, especially not the very special case of using consecutive integers as keys — if that was the case, we'd use an array — and I propose once again to make the default export Map-based
What do you think?
If you do not agree, it's fine! But I feel like repos such as
fastify-rate-limit
would greatly benefit from the changeThe text was updated successfully, but these errors were encountered: