Skip to content
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

Fixed single arg flaw with string types. #65

Merged
merged 2 commits into from
Feb 6, 2018
Merged

Fixed single arg flaw with string types. #65

merged 2 commits into from
Feb 6, 2018

Conversation

anywhichway
Copy link
Contributor

Your current code is subject to this flaw: https://codeburst.io/a-key-to-keys-when-javascript-keys-dont-match-ab44c81adc87

The pull request fixes the flaw with marginal impact on performance.

@anywhichway
Copy link
Contributor Author

This probably failed your build because I added a unit test that will fail with your legacy code.

@anywhichway
Copy link
Contributor Author

This pull request includes the previous pull request to fix the string key flaw. It also more than doubles the speed of fast-memoize in the single argument case. My guess is you will get a jump in multi-arg as well, but I don't thoroughly understand your benchmark suites, so I will leave it up to you to check.

@caiogondim caiogondim merged commit 6413dbb into caiogondim:master Feb 6, 2018
@megawac
Copy link

megawac commented Jun 21, 2018

This change broke my use of fast-memoize with a custom cache with a WeakMap as I implemented my checks to see whether the value exists using has and now has does not get called. It also makes the example superfluous as there is no need to implement a has method there.

if (!cache.has(cacheKey)) {
var computedValue = fn.call(this, arg)
var computedValue = cache.get(cacheKey);
if (typeof computedValue==="undefined") {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be sacrificing API correctness for speed.

Copy link
Contributor Author

@anywhichway anywhichway Jun 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@megawac I would say most of my approach does sacrifice API correctness for speed, but the stated goal of the library is to provide the "fastest possible memoization library in JavaScript that supports N arguments." I was just working in concert with its stated goal. Also, the failure to treat string keys correctly was in my opinion a fundamental semantic/functional flaw. Take a look at: https://codeburst.io/a-key-to-keys-when-javascript-keys-dont-match-ab44c81adc87.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants