fast-memoize.js
fast-memoize.js copied to clipboard
Remove inverted lookup and return on cache set
A couple of small optimisations:
- Invert the cache lookup, to avoid an extra logical operation
- Return on set cache
- Use that
return
to give the computed value back
Before | After |
---|---|
![]() |
![]() |
That's amazing. You care to make a release on npm? Will give you access to it.
I added you as collaborator on the project. You can merge. I see it as a minor release.
To make a new minor version do:
npm version minor
To release it on npm:
npm publish
I still need your username on npm to give you access.
You will probably get close to double the speed of the single arg memoizing where fast-memoize is highly effective if you make the change below and remove the 'has' call. However, my tests show this will have minimal benefit on multiple argument functions because most of the time is spent in JSON.stringify and string comparisons.
var computedValue = cache.get(cacheKey); if (typeof computedValue !=="undefined") { computedValue = fn.call(this, arg) cache.set(cacheKey, computedValue) } return computedValue;
If you modify the set spec to return the newly set value you will also be able to eliminate one line of code and an assignment.
You could also remove the typeof test and just let the code recompute when values from get are falsy, which will probably be just a small percentage of the time.
I have made the changes that double the speed and submitted a pull request.
Any reason why this has not been merged? Does this project need a new maintainer?
This clearly fell off my radar at the time, and now it’s too late to harmonise changes from 6 years ago, so I’ll close this.