fast-memoize.js
fast-memoize.js copied to clipboard
Doesn't memoize the result for keys returning `undefined`
When the memoized function returns undefined
for a specific parameter, another call to the memoized function runs the original function again as opposed to fetching the result from the cache.
Reproducible Example:
const memoize = require('fast-memoize')
const assert = require('assert')
let numCalled = 0
function longProcess(arg) {
++numCalled
return
}
// memoize result
const memoizedProcess = memoize(longProcess)
memoizedProcess("foo")
// get memoized result
memoizedProcess("foo")
assert(numCalled === 1)
It seems like this changed from v2.2.8
to v2.3.0
. Is this the intended behaviour?
I see how this could be the expected behaviour though, i.e. if the function returns undefined
, it could mean something in the function failed and it should be run again, e.g. it run before a global object was initialised/some data wasn't fetched yet.
@hargasinski have you benchmarked using a Map
cache with .has()
or .hasOwnProperty
/in
with the current null-prototype object cache?
Hmmm, in my opinion this is an unexpected behaviour if not explicitly stated otherwise.
I agree that it should be documented somewhere.
I checked the source code (line 36) and looks like it calls the original function whenever the cached result is undefined
.
(Usually, undefined
means something is not in the cache yet, so it calls the original function and caches this result. However, this means it no longer memoizes if the original function actually returns undefined
).
https://github.com/caiogondim/fast-memoize.js/blob/master/src/index.js#L36