Originally published at www.carloscaballero.io on February 8, 2019.
What means Memoization?
The definintion of memoization from th...
For further actions, you may consider blocking this person and/or reporting abuse
Good job, important concepts explained with easy words.
Small improvements to your
memoize
method:undefined
is actually a valid value for a function response. Probably should usehasOwnProperty
instead ofcache[stringifiedParams] === undefined
Thanks for the article.
Hi Enrique!
Thanks for your words, the intend in this post is show the memoization concept (I think that you can find in your language/framework/tool this technique to applied in your project).
Respect your words in the
memoize
method:hasOwnProperty
as you said!At the moment, I could to do a new version to teach in my class. Thanks very much for your comments :-).
I find it's best to use a
Map
or, for functions and other objects, aWeakMap
.The way I write my code, structural comparison will give almost no advantage, but
JSON.stringify
of huge objects will add overhead to every call (even a cache hit)Hi Mihail!
Good catch! In fact, the lodash implementation (github.com/lodash/lodash/blob/508d...) use map.
In my article, I only wanted show the memoization technique when you've a pure function!
In case that you want use it in a real project, I think that lodash implementation is a good option.
I have so many comments about that function :/
memoize.Cache
, but can't set it in the call tomemoize
(likememoize(fn, resolver, cacheClass)
)memoize(fn, resolver, cacheInstance)
, which would be more flexible and no less ergonomic.memoized.cache = cache.set(key, result) || cache
What if my custom cache returns a truthy value that isn't itself? For example if it returnsresult
value? Why not just set it tocache
, what does this achieve? Supporting an immutable cache?The closest I'd go with is something like this:
I don't think
this
support should be there by default, especially passingthis
both to the resolver and the function.Maybe this 😂👌:
Obviously a combinatorial explosion, but that is my point - most of the time you won't use most of the features anywhere in your entire project, so it's better to write your own function. Maybe you want to use a
WeakMap
ofMap
s, or vice versa, instead of a resolver.Memoization is already not a business logic requirement but a performance optimization. So why waste performance on a suboptimal generic solution instead of improving your algorithm by inlining a cache?
Hey, nice article. I've implemented a slightly tweaked version of memoization as well, you can read more about that here: dev.to/ritikesh/why-just-cache-whe....
Cool article!
How did you generate source code example images?
I'm using carbon which you can find as VSCode plugin.
Thanks!
Thanks, I wanted to know that too.
Great article
Really interesting post! I am wondering though- what is "TurboFan"? 🤔
As this wasn't answered and I was also interested, I did a quick Google and found this:
v8.dev/docs/turbofan
"TurboFan is one of V8’s optimizing compilers leveraging a concept called “Sea of Nodes”. "
Obviously, V8 is the name of Google Chrome's JavaScript interpreter, which is what NodeJS is based on.