fast-memoize.js icon indicating copy to clipboard operation
fast-memoize.js copied to clipboard

Remove inverted lookup and return on cache set

Open gnclmorais opened this issue 8 years ago • 4 comments

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
image image

gnclmorais avatar Jan 27 '17 10:01 gnclmorais

That's amazing. You care to make a release on npm? Will give you access to it.

caiogondim avatar Jan 27 '17 13:01 caiogondim

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.

caiogondim avatar Jan 27 '17 13:01 caiogondim

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.

anywhichway avatar Jan 30 '18 19:01 anywhichway

I have made the changes that double the speed and submitted a pull request.

anywhichway avatar Feb 02 '18 00:02 anywhichway

Any reason why this has not been merged? Does this project need a new maintainer?

gamedevsam avatar Apr 06 '23 05:04 gamedevsam

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.

gnclmorais avatar Apr 06 '23 09:04 gnclmorais