-
Notifications
You must be signed in to change notification settings - Fork 178
Use JSON.stringify for cache key generation #42
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
Use JSON.stringify for cache key generation #42
Conversation
|
Funky - I was just trying this out too! I didn't try storing a reference to stringify though, that's a really strange performance thing. I set up a benchmark to check these: https://esbench.com/bench/5c18ff474cd7e6009ef61e65 |
|
A small note: in the inline loop and reduce there needs to be a non-number character between
If the lengths are not separated by a some (non-number) character then both of them have the cache key |
|
heh yeah I noticed that! updated the benchmark too. I replaced the map() test with a slight improvement on the loop one: instead of initializing key to an empty string, initializing it to a non-numeric character seems to trigger more optimal key lookups, at least in V8. |
|
Interesting. Is the updated benchmark accessible somewhere? The one linked above still seems to have the |
|
forgot to save - it's updated now |
Prefix the key with a non-numeric character, as this seems to give a speed-up on Chrome.
|
Modified the cache key creation based on benchmarks. Now the key is a run-length encoding of statics. The key is also prefixed with a non-numeric character, which seems to give a boost on Chrome. Added tests. Edit: It's a bit too late to change the commit message, so let's mention here that when I wrote "run-length encoding" I actually meant length-prefixed strings. RLE means something different. |

This pull request modifies the cache key generation to use
JSON.stringify. With this the cache key is guaranteed to be unique for every unique array of statics, as two different arrays should not have identical JSON serializations.JSON.stringify is now stored to a module-scoped variable, to cut down the size. This seems to give a (surprising) performance boost in my environment. I suspect this is highly JavaScript engine specific. Benchmarks below.
Before change:
Creation: 36985/s, average: 0.027037810036229928msAfter change:
Creation: 45726/s, average: 0.02186895613191043msThe compressed code size also decreased somewhat.
Before change:
After change:
Storing
JSON.stringifyin a variable but keeping the cache key generation intact (statics.length + statics) seems to yield the same performance, with a slightly larger gzipped code (624B instead of 622B). Uniqueness guarantee for cache keys is also lost.