Optimized replacement for _.memoize() #330
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I took the work we've been doing on an optimal memoization routine in [1][2] and integrated it with underscore to offer an implementation which is both:
The implementation in [2] comes with it's own tests in that repo, however what you're probably more interested in looking at are the performance figures. Here's a jsPerf test (others are available) which demonstrates the improvements in performance from the existing implementation when compared to this one:
http://jsperf.com/comparison-of-memoization-implementations/12
The work here has gone through multiple rounds of further optimization by @mathias, @DmitryBaranovskiy, @JamieMason and others and is currently the fastest (thus benchmarked) memoizer we're currently aware of (as you'll see in the tests, we also compare against other implementations by John Hann, Thomas Fuchs etc).
The only real difference between the current approach and mine/ours is that the optional hash function is dropped. I've thoroughly reviewed use of memoization in JS projects out there at the moment and I haven't seen a great number of people actually rely on such optional functions, they just want the main memoizer itself.
It would be great if we could get this in Underscore, but definitely open to thoughts on whether it's worth including.
Notes:
The new memoizer has dependencies on JSON support being natively available [4]. I think this is okay, but we're happy to spend more time on support issues if needed. I'm currently looking at putting together a compact stringify method that could be substituted to resolve any compat issues for a future update.
You may also ask whether the perf test is a fair comparison of access/usage times. We've also separately benchmarked these in the earlier stages of development with the newer implementation outperforming the others there too. Happy to create another test to demonstrate if needed.
Thanks!
Refs:
[1] http://addyosmani.com/blog/faster-javascript-memoization/
[2] https://github.com/addyosmani/memoize.js
[3] http://jsperf.com/comparison-of-memoization-implementations/12
[4] http://caniuse.com/json (browser support levels for JSON)